Sign in to follow this  

dynamic_cast help

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

Guest Anonymous Poster
to do a dynamic cast from b to a
a has to be inherited from b and b has to have a virtual destructor

Share this post


Link to post
Share on other sites
Quote:
Original post by Hermes
Object* a = new Object(40, 'a');
delete a;
void* b = new Object(40, 'b');
a=dynamic_cast<Object*>b;//why can't i do this?
delete a;

Thank you!

Firstly,
a=dynamic_cast<Object*>b
should be
a=dynamic_cast<Object*>(b)

Secondly, the compiler can't determine what type the object is because your providing it with a void*, which provides absolutely no information to the compiler about the type of the object. The compiler at least needs to know what class heirachy the object is part of. A better example of dynamic_cast would be

ObjectBase* b = new Object(40,'b');
Object* a = dynamic_cast<Object*>(b);

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
to do a dynamic cast from b to a
a has to be inherited from b and b has to have a virtual destructor

A virtual destructor isn't necessary for dynamic_cast, but it's a good idea none-the-less.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hermes
Object* a = new Object(40, 'a');
delete a;
void* b = new Object(40, 'b');
a=dynamic_cast<Object*>b;//why can't i do this?
delete a;


i can't see why you really must need to do this but if you really do then dynamic_cast is not the one for you, dynamic_cast is used to navigate type hierarchies of related types, you wont a cast that lets you cast between un-related types which is reinterpret_cast e.g


Object* a = new Object(40, 'a');
delete a;

void* b = new Object(40, 'b');
a= reinterpret_cast<Object*>(b);
delete a;

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
Quote:
Original post by Anonymous Poster
to do a dynamic cast from b to a
a has to be inherited from b and b has to have a virtual destructor

A virtual destructor isn't necessary for dynamic_cast, but it's a good idea none-the-less.

From what I've heard, lots of implementations uses the v-table to check on types and when using dynamic_cast, so you'll need at least one virtual function.. and as a general rule, when at least one function is virtual, the destructor should be virtual, too.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jolle
From what I've heard, lots of implementations uses the v-table to check on types and when using dynamic_cast, so you'll need at least one virtual function.

That could very well be true, it does sound familiar.

Quote:
and as a general rule, when at least one function is virtual, the destructor should be virtual, too.

Yep, hence me mentioning that the virtual destructor was a good idea anyway. I was really just nit-picking [smile]

Share this post


Link to post
Share on other sites

This topic is 4845 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this