faqts : Computers : Programming : Languages : C++

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 1 of 2 people (50%) answered Yes

Entry

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.
Referring to
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_78.asp
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.