Jump to content
  • Advertisement
Sign in to follow this  
MTclip

multiple inheritance and _casts

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

ok real quick..
class base1;
class base2;

class derived1 : base1;

class multiderive : derived1, base2;

base1* myClass = new multiderive()

static_cast<base2*>(myclass);

as you can see i am tring to get at the other base class.. ..for storage purposed.. anyways, the static_cast does not work.. it has to be possible to do this.. im thinking one of the other casts.. dynamic reinterpret is that right? ..

Share this post


Link to post
Share on other sites
Advertisement
A dynamic_cast will work, and so will static_casting it to a multiderive and then static_casting that pointer to a base2, but you had better be darn sure that it's a multiderive pointer that you have there (and if you are darn sure, then why do you have a base1 pointer in the first place?). A reinterpret_cast will probably do the wrong thing, that is reinterpret the bit pattern of the base1 pointer as a base2 pointer, which will most likely not be the correct index into the complete object, causing bad mojo.

Share this post


Link to post
Share on other sites
well i tried them both..
both compiled
dynamic.. had runtime error
reinterpret.. worked just fine

should dynamic work..?



edit:: ahh ok... i started a reply before your post... well ill see

Also ... does casting call constructors and destructors... ?

Share this post


Link to post
Share on other sites
If you use dynamic_cast, make sure your base classes have virtual functions. In particular, classes intended to be used a base class should have virtual destructors.

Casts on pointers should not call constructors or destructors. However, some casts on non-pointer types can generate temporaries. For example a static_cast<std::string> on a char * will generate a temporary string object.

Share this post


Link to post
Share on other sites
A reinterpret_cast would be dangerous to use here. Inside the multiderive class, the storage for base1 and base2 begin at different addresses, but a reinterpret_cast won't adjust your pointer correctly because it can assume nothing about how the two base classes are connected through multiderive.

If you want to convert from base1 to base2 without knowing for sure whether your pointer to base1 really points to a class of type multiderive, then you need to use dynamic_cast, but this has performance implications.

If you want to convert from base1 to base2, and you actually know (somehow) that your pointer to base1 really points to a class of type multiderive, then I recommend doing this:

base2 *foo = static_cast<multiderive *>(myClass);

This uses static_cast to go from base1 to multiderive, and then the conversion to base2 happens implicitly.

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!