For example we have an Animal class ,and we created some other class such as Lion class,Tiger class,etc. I have made a list of the Animal class and I want to transverse the list and process the list according to the type of the class of the every member in the list.
Class Animal
Class Tiger :: public Animal{}
Class Lion :: public Animal{}
list<Animal> l;
Tiger T;
Lion L;
l.push_back(T); l.push_back(L);
if the top member of the list is Tiger print"ITs a tiger"
else print"something"
Simply, I want to check the type of the instance created. I don’t know how to do it.
This is called RTTI and it’s not good coding practice.
This being said, if you absolutely want to know a class’ type, you can do
What I would recommend in your case is to have a common interface to all
Animal, for instance asayHello()method. You would haveIn
Tigerthis would beThen, from your
vector<Animal*>(you need to use pointers) just call