You do not need to know what type of subclass you have. You do not perform polymorphism correctly if you need to check the type of class you are dealing with. The whole point of polymorphism is to reduce, if make your code much more flexible.
However, there are times when you need to know, and you can use RTTI for this, however I recommend not to do this, especially if you need (for example, games or graphics programs).
Use the typeid operator to get information about the class and determine if the class is a particular type.
For instance:
Animal* animal1 = new Cat; if(typeid(animal1) == typeid(Cat)) { cout << "animal1 is a: " << typeid(Cat).name(); }
Then use static_cast to reset it using hiearchy.
Animal* animal1 = new Cat; if(typeid(animal1) == typeid(Cat)) { Cat* cat = static_cast<Cat*>(animal1); cat->scratchTheLivingHellOutOfYou(); }
You can use dynamic_cast , which is much safer, but much slower than typeid / static_cast. For instance:
Animal* animal1 = new Cat; if(Cat* cat = dynamic_cast<Cat*>(animal1) { cat->scratchTheLivingHellOutOfYou(); }
EDIT:
dynamic_cast slower simply because it needs to do a little extra work than just testing if it's a specific type and casting. those. dyanmic_cast not equivalent to typeid/static_cast , but it is almost equal.
Imagine deeper depths than 2 levels, for example:
class Animal { /* ... */ }; // Base class Cat : public Animal { /* ... */ }; // 2nd level class Tiger : public Cat { /* ... */ }; // 3rd level
Let's say that in the Cat class, a method specific to all cats is called: scratchTheLivingHellOutOfYou() . Let me also say that: I have an Animal * list, and I want to call scratchTheLivingHellOutOfYou() for each Cat in the list (this includes classes that are derived from the Cat class). If the typeid operator and static_cast , this will not lead to what is required. Because typeid only checks the current type and does not care about the hierarchy. To do this, you need to use dynamic_cast , as it checks whether the class is derived from the base class, and then flip up / down the hierarchy accordingly.
You can see this simple example in C ++, here . Here is the output of the program:
USING TYPEID *scratch* meoyawnn! RAWR USING DYNAMIC_CAST *scratch* meoyawnn! *scratch* RAWR
Therefore, you can clearly see that dynamic_cast works much more than simple typeid and static_cast . Because dynamic_cast looks through hiearchy to see if it is a particular type. Simply put ... dynamic_cast can move up and down the hierarchy. While typeid and static_cast can drop the hierarchy only for a specific type.
I think I would say that if dynamic_cast does not work, it will return a NULL pointer or throw an exception if you use it with references.
NOTE:
- If you really need performance, and you need to check the types of polymorphic objects, I recommend finding an alternative to RTTI. For example, using templates / macros or something else to identify classes.
dynamic_cast should be used only if you are not sure if the object will be the type you are converting to. If you, as a programmer, know what you are doing, 100% will be that type, then use static_cast , for example. if you know that animal1 will be Cat , then static_cast more appropriate.