how do I find the class that an object is an instance of?
Oct 23rd, 2003 05:27
Colin Thomsen, Guest,
To find out run-time information about instances of classes, you need to
enable Run-Time Type Information (RTTI) in your build settings.
Different compilers have different ways of turning this option on.
Once the compile option is turned on, you can find out information about
objects using the typeid operator.
an example code snippet is:
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid( pb ).name() << endl; //prints "class Base *"
cout << typeid( *pb ).name() << endl; //prints "class Derived"
The typeid operator returns a type_info class which contains the name.