Mega Code Archive

 
Categories / C++ Tutorial / Development
 

Use a reference with typeid

#include <iostream> #include <typeinfo> using namespace std; class Base { public:   virtual bool lays_eggs() { return false; } // Base is polymorphic }; class Derived1: public Base { public: }; class Derived2: public Base { public:   bool lays_eggs() { return true; } }; void WhatBase(Base &ob) {   cout << "ob is referencing an object of type ";   cout << typeid(ob).name() << endl; } int main() {   Base AnyBase;   Derived1 Derived1;   Derived2 Derived2;   WhatBase(AnyBase);   WhatBase(Derived1);   WhatBase(Derived2);   return 0; } ob is referencing an object of type 4Base ob is referencing an object of type 8Derived1 ob is referencing an object of type 8Derived2