Jump to content
  • Advertisement
Sign in to follow this  
MTclip

casting... for temporary access

This topic is 4863 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

base * pObject = new Derived(); how would i cast pObject to a Derived... dynamic static reinterpret.. ??

Share this post


Link to post
Share on other sites
Advertisement
That depens on the context...

If you are sure, it can only be of this type you can use static...

If you are not so sure, you should use a dynamic cast, so that you will get an exception if the cast fails.

Share this post


Link to post
Share on other sites
The only way to know if youre cast is possible is to store in the class a pointer to the parent class cause C++ stores no type information.

Share this post


Link to post
Share on other sites
Quote:
Original post by dali
The only way to know if youre cast is possible is to store in the class a pointer to the parent class cause C++ stores no type information.


*WRONG* assuming that RTTI is enabled as it should be by default since it's part of the language dynamic_cast will work for this with the caveat that you need to somewhere in base have a virtual function.

Share this post


Link to post
Share on other sites
For the above code, use static_cast. dynamic_cast isn't necessary there. reinterpret_cast should be used for casting to unrelated pointers. A base class pointer is related to a derived class pointer.

Share this post


Link to post
Share on other sites
After using some form of RTTI, and determining the actual type of your given object, you should use a dynamic_cast. Strictly speaking, you should also check if the return is 0 to make sure it succeeds. An assert is probably best for this check, which will disappear in a release build.

NOTE: You can only make use of dynamic_cast if the base class in question has at least one virtual method.

Share this post


Link to post
Share on other sites
Quote:
Original post by DigitalDelusion
Quote:
Original post by dali
The only way to know if youre cast is possible is to store in the class a pointer to the parent class cause C++ stores no type information.


*WRONG* assuming that RTTI is enabled as it should be by default since it's part of the language dynamic_cast will work for this with the caveat that you need to somewhere in base have a virtual function.


Youre right ;) But letting the compiler do it is much slower right?

Share this post


Link to post
Share on other sites
Quote:
Original post by daliYoure right ;) But letting the compiler do it is much slower right?


Have you measured it? Does your implementation work seamlessly for any type under both multiple and virtual inheritance? You're assuming that a language feature is unneeded and slow an assumption I would say is probably wrong.

And in any case you shouldn't be type switching in the first place, double dispatch or similar strategies should be used to avoid such monstrosities.

Share this post


Link to post
Share on other sites
I assume its slower cause compiler support must handle multiple inheritance so the RTTI system can be slower than one designed specifically for single inheritance. Moreover compiler support is typically a nonportable solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by dali
I assume its slower cause compiler support must handle multiple inheritance so the RTTI system can be slower than one designed specifically for single inheritance. Moreover compiler support is typically a nonportable solution.


If your application bottleneck is type-swithing then I would say you're doing something awfully wrong and in any case relying on a langauge feature is at least as portable as hacking something togheter yourself and robustly handling even single inheritance really isn't to easy not to mention all the time you would spend administrating type id:s instead of just letting the compiler figure it out for you, be lazy it's healthy.

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!