• Advertisement

Dynamic typecast - basic newbie c++ quick question

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

Kindof embarrassing since I don't consider myself a newbie, but I guess I'm still learning OOP. I have tried Google for all I'm worth and can't find the answer to this simple question: I've got a class: "entity". And I've got a subclass of it, called "creature". Now I've got a pointer to some kind of "entity". And I want to know if that particular object is a "creature", so I dare access members which only creatures have. How do I do that? I remember that you should attempt to do some kind of typecast and check if it worked, kindof like this:
 if ((creature*)entitypointer != NULL)
then_this_is_a_creature();

But the typecast never becomes NULL no matter which kinds of "entities" I pass. How do I do this correctly?

Share this post

Share on other sites
Advertisement
Try:
creature *the_creature = dynamic_cast <creature *> (entitypointer);if (the_creature != 0){  // do stuff with the_creature (as long as it doesn't break any local byelaws)}

Skizz

Share this post

Share on other sites
You're looking for dynamic_cast.

Share this post

Share on other sites
Woah that was quick, thanks guys!

Share this post

Share on other sites
Doesn't work off the bat, and I'm afraid I'm going to have to ask again. I don't understand why the compiler throws this error:

error: cannot dynamic_cast 'holder' (of type 'class entity*') to type 'class creature*' (source type is not polymorphic)

According to me they should be polymorphic, because I've declared the classes like this:
class entity { ... };class creature : public entity  { ... };

And the creature class has some functions and variables which are not at all mentioned in the entity class. I don't have to mention them there somehow, like virtual or something, do I? What is my error?

Share this post

Share on other sites
dynamic_casts only work with polymorphic types, as the error message suggested. To convert pointers to non-polymorphic types use static_cast instead.

This leads to the question: are you aware of the difference between polymorphic and non-polymorphic types?

Skizz

Share this post

Share on other sites
There needs to be at least one virtual function, so that the classes have some polymorphic behaviour. (And anywhere that you have this, you should be sure the destructor is virtual as well, even if it doesn't need to do any work. While you're at it, consider if you need to work on the copy constructor or assignment operator as well.)

Of course, the usual idea is to try really hard to make proper use of virtual functions (and once you have one, you've paid the price, so go nuts - with the usual caveats about good design of course) so that you don't *need* to dynamic_cast. I.e., actually leverage the polymorphic nature of the classes.

Share this post

Share on other sites

• Advertisement
• Advertisement
• Popular Tags

• Advertisement
• Popular Now

• 10
• 14
• 9
• 9
• 11
• Advertisement