C++ Tutorial/Development/typeid

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Compare the typeid for templates

<source lang="cpp">#include <iostream> using namespace std; template <class T> class MyClass {

 T a;

public:

 MyClass(T i) { a = i; }

}; int main() {

 MyClass<int> o1(10), o2(9);
 MyClass<double> o3(7.2);
 if(typeid(o1) == typeid(o2))
   cout << "o1 and o2 are the same type\n";
 if(typeid(o1) == typeid(o3))
   cout << "Error\n";
 else
   cout << "o1 and o3 are different types\n";
 return 0;

}</source>

o1 and o2 are the same type
o1 and o3 are different types

Demonstrating run-time type id

<source lang="cpp">#include <iostream> using namespace std; class Base { public:

 virtual bool method() { return false; } // Base is polymorphic

}; class Derived1: public Base { public: }; class Derived2: public Base { public:

 bool method() { return true; }

}; class Derived3: public Base { public: }; Base *factory() {

 switch(rand()  3 ) {
   case 0: return new Derived3;
   case 1: return new Derived1;
   case 2: return new Derived2;
 }
 return 0;

} int main() {

 Base *ptr;
 for(int i=0; i<10; i++) {
   ptr = factory(); // generate an object
   cout << "Object is " << typeid(*ptr).name();
   cout << endl;
   if(typeid(*ptr) == typeid(Derived3))
      cout << " Derived3";
   if(typeid(*ptr) == typeid(Derived1))
      cout << " Derived1";
   if(typeid(*ptr) == typeid(Derived2))
      cout << " Derived2";
 }
 return 0;

}</source>

Object is 8Derived2
 Derived2Object is 8Derived2
 Derived2Object is 8Derived1
 Derived1Object is 8Derived1
 Derived1Object is 8Derived2
 Derived2Object is 8Derived1
 Derived1Object is 8Derived3
 Derived3Object is 8Derived3
 Derived3Object is 8Derived1
 Derived1Object is 8Derived2
 Derived2"

Output the name of a typeid

<source lang="cpp">#include <iostream>

  1. include <typeinfo>

using namespace std; int main () {

 int * a,b;
 a=0; b=0;
 if (typeid(a) != typeid(b))
 {
   cout << "a and b are of different types:\n";
   cout << "a is: " << typeid(a).name() << "\n";
   cout << "b is: " << typeid(b).name() << "\n";
 }
 return 0;

}</source>

print type name of what ptr points at

<source lang="cpp">#include <iostream>

  1. 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;

}</source>

typeid() function and RTTI must be enabled in compiler

<source lang="cpp">#include <iostream>

 #include <typeinfo>             
 using namespace std;  
 class Base  
   {  
    virtual void virtFunc()    
       {  }  
   };  
 class Derv1 : public Base  
   { };  
 class Derv2 : public Base  
   { };  
 void displayName(Base* pB)  
   {  
    cout << "pointer to an object of ";  //display name of class  
    cout << typeid(*pB).name() << endl;  //pointed to by pB  
   }  
 
 int main()  
   {  
    Base* pBase = new Derv1;  
    displayName(pBase);   //"pointer to an object of class Derv1"  
   
    pBase = new Derv2;  
    displayName(pBase);   //"pointer to an object of class Derv2"  
    return 0;  
   }</source>

Use a reference with typeid

<source lang="cpp">#include <iostream>

  1. include <typeinfo>

using namespace std; class Base { public:

 virtual bool lays_eggs() { return false; } // Base is polymorphic

}; class Derived1: public Base { public: }; class Derived2: public Base { public:

 bool lays_eggs() { return true; }

}; void WhatBase(Base &ob) {

 cout << "ob is referencing an object of type ";
 cout << typeid(ob).name() << endl;

} int main() {

 Base AnyBase;
 Derived1 Derived1;
 Derived2 Derived2;
 WhatBase(AnyBase);
 WhatBase(Derived1);
 WhatBase(Derived2);
 return 0;

}</source>

ob is referencing an object of type 4Base
ob is referencing an object of type 8Derived1
ob is referencing an object of type 8Derived2

Use typeid

<source lang="cpp">#include <iostream>

  1. include <typeinfo>

using namespace std;

class MyClass { };

int main() {

 int i, j; 
 float f; 
 MyClass ob; 

 cout << "The type of i is: " << typeid(i).name() << endl; 
 cout << "The type of f is: " << typeid(f).name() << endl; 
 cout << "The type of ob is: " << typeid(ob).name() << "\n\n"; 

 if(typeid(i) == typeid(j)) 
   cout << "The types of i and j are the same\n"; 

 if(typeid(i) != typeid(f)) 
   cout << "The types of i and f are not the same\n"; 

 return 0; 

}</source>

The type of i is: i
The type of f is: f
The type of ob is: 7MyClass
The types of i and j are the same
The types of i and f are not the same

Use typeid on a polymorphic class heirarchy

<source lang="cpp">#include <iostream>

  1. include <typeinfo>

using namespace std;

class Base { };

class Derived1: public Base { };

class Derived2: public Base { };

int main() {

 Base *p, baseob; 
 Derived1 ob1; 
 Derived2 ob2; 

 p = &baseob; 
 cout << typeid(*p).name() << endl; 

 p = &ob1; 
 cout << typeid(*p).name() << endl; 

 p = &ob2; 
 cout << typeid(*p).name() << endl; 

 return 0; 

}</source>

4Base
4Base
4Base

Using typeid: class pointer, base class, derived class,

<source lang="cpp">#include <iostream>

  1. include <ostream>
  2. include <typeinfo>

class base { public:

 virtual ~base() {}

}; class derived : public base {};

int main() {

 base* b = new derived;
 std::cout << typeid(*b).name() << "\n";      
 std::cout << typeid(base).name() << "\n";    
 derived* d = new derived;
 std::cout << typeid(*d).name() << "\n";      
 std::cout << typeid(derived).name() << "\n"; 

}</source>

7derived
4base
7derived
7derived

Using typeid: enum data type

<source lang="cpp">#include <iostream>

  1. include <ostream>
  2. include <typeinfo>

enum color { red, black }; int main() {

 std::cout << typeid(red).name() << "\n";
 std::cout << typeid(color).name() << "\n";

}</source>

5color
5color

Using typeid with templates

<source lang="cpp">#include <iostream> using namespace std; template <class T> class MyClass {

 T a;

public:

 MyClass(T i) { a = i; }

}; int main() {

 MyClass<int> o1(10), o2(9);
 MyClass<double> o3(7.2);
 cout << "Type of o1 is ";
 cout << typeid(o1).name() << endl;
 cout << "Type of o2 is ";
 cout << typeid(o2).name() << endl;
 cout << "Type of o3 is ";
 cout << typeid(o3).name() << endl;
 return 0;

}</source>

Type of o1 is 7MyClassIiE
Type of o2 is 7MyClassIiE
Type of o3 is 7MyClassIdE