I would like to ask about something like "instanceof" in Java. I created a simple example of inheritence. My idea was made an abstarct class Monster and children Skeleton and Zombie, but with abstract it doesn't work, so We have std::vectorBase class. I pushed children objects into vector. I want to call method of children classes, but method which is called is base class empty method. Is existing any way to do it ? Maybe in c++ programming we should avoid this code thinking and do it using vector skeleton and vector zombie separately? Sorry for my english. I hope you understand my problem.
class Monster
{
public:
virtual void describe() {};
};
class Skeleton : public Monster
{
public:
Skeleton() {
}
~Skeleton(){}
void describe() override {
std::cout << "I am skeleton" << std::endl;
}
};
class Zombie : public Monster
{
public:
Zombie(){}
~Zombie(){}
void describe() override {
std::cout << "I am Zombie" << std::endl;
}
};
int main(void) {
std::vector<Monster> potwory;
potwory.push_back(Skeleton());
potwory.push_back(Zombie());
Skeleton sz;
Zombie z;
potwory.push_back(sz);
potwory.push_back(z);
for (auto i = 0; i < potwory.size(); i++) {
std::cout << typeid(potwory[i]).name() << std::endl; // each of them is Monster object
potwory[i].describe(); //here is calling method from base class , I want derived method.
}
std::cin.get();
return 0;
}`