# Dynamic typecast - basic newbie c++ quick question

## Recommended Posts

NQ    328
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
Skizz    794
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
Colin Jeanne    1114
You're looking for dynamic_cast.

#### Share this post

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

#### Share this post

##### Share on other sites
NQ    328
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
Skizz    794
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
Zahlman    1682
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.

## 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