So I'm learning RTTI. I have a factory method that returns random objects. Each time I check the count of each animal (derived from Mammal), it doesn't add up the dogs, cats etc. All it does is return Mammal. Why am I not getting the derived object names instead? Thanks.
Code:#include <iostream> #include <fstream> #include <string> #include <typeinfo> class Mammal { public: }; class Cat : public Mammal { public: }; class Platypus : public Mammal { public: }; class Dog : public Mammal { public: }; Mammal *factory() { switch (rand()%3) { case 0: return new Dog; case 1: return new Cat; case 2: return new Platypus; } return 0; } int main() { Mammal *ptr; int i; int c = 0, d = 0, p = 0; for (i = 0; i < 10; i++) { ptr = factory(); std::cout << "Object is " << typeid(*ptr).name() << std::endl; std::cout << std::endl; if (typeid(*ptr) == typeid(Dog)) d++; if (typeid(*ptr) == typeid(Cat)) c++; if (typeid(*ptr) == typeid(Platypus)) p++; } std::cout << "Cats: " << c << std::endl; std::cout << "Dogs: " << d << std::endl; std::cout << "Platypus: " << p << std::endl; system("pause"); return 0; }