Jump to content
  • Advertisement
Sign in to follow this  
irreversible

Smart pointer casting

This topic is 2057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!