• Typeid is a c++ keyword, and the return result of the typeid operator is a reference to the standard library type type_info object.

  • However, the C++ standard does not explicitly define type_info, and its implementation depends on the individual compiler. The standard only specifies that the TypeID operator must perform the following four operations:

operation instructions
t1 == t2 Return true if two objects T1 and T2 are of the same type; Otherwise return false
t1 ! = t2 Return true if two objects t1 and T2 are of different types; Otherwise return false
t.name() Returns a C-style string of type. Depending on the compiler, this may not be the actual type name
t1.before(t2) Determine whether T1 is in front of T2. The type order is compiler dependent; the base class does not necessarily precede the derived class.
  • The type_info member function name returns a c-style string of the type, but the type name returned may not be the same as the corresponding type name used in the program. The implementation of the return value is determined by the compiler. The standard only requires that each type return a unique string.

  • Like the sizeof operator, TypeID operates on objects that can be either data types or expressions.

  • Example code for determining various types using typeID is as follows:

#include<iostream>  
#include <typeinfo>  
using namespace std;  

class Base{};
class Derived:public Base{};
void func1(a);
int func2(int n);

int main(a)  
{  
    int a = 10;
    int* b = &a;
    float c;
    double d;

    cout << typeid(a).name() << endl;
    cout << typeid(b).name() << endl;
    cout << typeid(c).name() << endl;
    cout << typeid(d).name() << endl;
    cout << typeid(Base).name() << endl;
    cout << typeid(Derived).name() << endl;
    cout << typeid(func1).name() << endl;
    cout << typeid(func2).name() << endl;
}  
Copy the code
  • The result of clang++ compilation on Mac is as follows:
i
Pi
f
d
4Base
7Derived
FvvE
FiiE
Copy the code
  • Unlike dynamic languages such as Java and C#, the C++ runtime has very limited type information and the standards are vaguely defined. In practice, we generally use only the “==” operator of type_info to determine whether two types are the same.
  • Take a look at the following example code:
#include<iostream>  
#include <typeinfo>  
using namespace std; 

class Base{};
class Drived: public Base{};

int main(a)
{
    Base* pb;
    Drived d;
    pb = &d;

	if(strcmp(typeid(*pb).name(), typeid(Base).name()) = =0)
    {
        cout << "this is Base" << endl;
    }
    else if(strcmp(typeid(*pb).name(), typeid(Drived).name()) = =0)
    {
        cout << "this is Drived" << endl;
    }
	
	if(strcmp(typeid(d).name(), typeid(Base).name()) = =0)
    {
        cout << "this is Base" << endl;
    }
    else if(strcmp(typeid(d).name(), typeid(Drived).name()) = =0)
    {
        cout << "this is Drived"<< endl; }}Copy the code
  • The result of clang++ compilation on Mac is as follows:
this is Base
this is Drived
Copy the code
  • As can be seen from the run result, even if the base class pointer is used to point to a derived class, the base class pointer type determined by using TypeID is still the base class pointer type. Therefore, we cannot use TypeID to determine whether the base class pointer actually points to a derived class.