Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ typeid operator

I am using Visual Studio 2005 Proffesional Edition.

In the following example SomeClass is class that is defined in third party dll library I am using. SomeClass has virtual methods. I noticed that the operator typeid gives different results when applied to the type itself, and when applied to object of the type. Is this normal behavior, and if not what could be the reason for such behavior?

typeid(SomeClass).raw_name()   // the value of this is   ".?AVSomeClass@@"
typeid(SomeClass).name()          ///  "class SomeClass"

SomeClass obj;
typeid(obj).raw_name(); // ".?AVTLomeClass@@"
typeid(obj).name();       // "class TLomeClass"
like image 768
user152508 Avatar asked Oct 25 '22 23:10

user152508


1 Answers

Is the code in your question the same or similar to the code you are having problems with?

Operator typeid, when it is applied to polymorphic types, returns the type_info object that identifies the dynamic type of the polymorphic object. So, for example, if you apply typeid to a reference of type Base & (where Base is polymorphic), which is actually bound to an object of type Derived (where Derived is derived from Base), the type_info object returned by typeid will correspond to the Derived class, not to Base class. Could it be that something like that is happening in your code?

Also, in some compilers (like MS Visual Studio) in order to use fully-functional typeid, as described above, you need to compile your code with Run-Time Type Information (RTTI) enabled. Maybe the absence of RTTI led to the strange effects you observed.

P.S. Contrary to what is stated in the currently accepted answer, typeid is fully and perfectly standard C++ feature. It is not a compiler extension.

like image 184
AnT Avatar answered Nov 12 '22 22:11

AnT