Jump to content

  • Log In with Google      Sign In   
  • Create Account


Smart pointer casting


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 irreversible   Crossbones+   -  Reputation: 1316

Like
0Likes
Like

Posted 30 November 2012 - 08:42 AM

Being an idiot and not working on the clock, I get more of a kick out of doing stuff myself rather than using existing libraries. I'm currently slowly introducing smart pointers into my codebase and the main thing I've been kind of trying to work out in the most efficient/flexible manner is casting.

There are a number of ways to implement smart pointer casting, each with a slightly different syntax and ease of use/clutter. As far as I know there's no generic way to implement casting from a base class to derived class so direct assignment becomes possible as an automatic feature. If anyone has further insight into this, I'd appreciate it.

Currently, I've opted to implement casting via a separate custom function, not operator, which calls the constructor for a new pointer while retaining all allocated memory pointers (note that the A parameter is related to my memory manager):

template<class T>
ISharedPtr {

   ...

   //the cast function
   template<typename CastType>
   ISharedPtr<CastType> Cast() { return ISharedPtr<CastType>((CastType*)ptr, iRefCount, iSize, (ISharedPtr<CastType>*)A); }
   };

//casting now becomes:

ISharedPtr<IBase> base;
ISharedPtr<IDerived> = base.Cast<IDerived>();

To me this syntax is concise and to the point. Nevertheless, it's cumbersome and looks unintuitive due to the trailing nature of the cast. The word cast itself implies putting something into something,eg int(10.f), not taking something out of something.

How would/did you implement this and if you have any further thoughts on the topic, do share!

Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3942

Like
1Likes
Like

Posted 30 November 2012 - 09:03 AM

Setting aside I would not implement something that is already in the standard library, I would do the casting as the standard library does. Most importantly I would not do a C style cast. It loses all type safety for absolutely no reason.

#3 irreversible   Crossbones+   -  Reputation: 1316

Like
0Likes
Like

Posted 04 December 2012 - 01:53 AM

Thanks for the input! I'm personally not too fond of the syntax of the different cast operators - they're a bit long and cumbersome when used inline. :)

#4 wqking   Members   -  Reputation: 756

Like
2Likes
Like

Posted 04 December 2012 - 02:19 AM

Seems you are implementing your own version smart pointer, then you should implement your own version of static_pointer_cast, dynamic_pointer_cast, and const_pointer_cast.

You can dig into Boost source code to see how its smart pointer doing that.

Also, since you are writting C++ code, you'd better adopt the "long" caster operators. Longer line is OK, losing type safe with C style cast is not quite OK.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS