casting... for temporary access

Started by
23 comments, last by dali 18 years, 9 months ago
base * pObject = new Derived(); how would i cast pObject to a Derived... dynamic static reinterpret.. ??
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.
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.
Gaudeamus igitur, iuvenes dum sumus!
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.
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.
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?
Gaudeamus igitur, iuvenes dum sumus!
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.

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.
Gaudeamus igitur, iuvenes dum sumus!
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement