Mega Code Archive

 
Categories / C++ Tutorial / Development
 

Print type name of what ptr points at

#include <iostream> #include <typeinfo> using namespace std; class Base { public:   Base():hide(0){}   void mutate(int i){ hide = i;}   void print()const {cout << "hide in Base = "                    << hide << endl;} private:   int hide; }; class One: public Base { public:   One():Base(),data(0) {}   void mutate(int i){ data = i;}   void print()const {cout << "data in One = "                            << data << endl;} private:   int data; }; int main () {    Base* bptr; One* Derived;    cout << typeid(bptr).name() << endl;    cout << typeid(Derived).name() << endl;    if (typeid(bptr) == typeid(Derived))       cout << "type bprt & Derived same" << endl; }