C++/Development/typeid

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

An example that uses typeid for base and derived classes

<source lang="cpp">

  1. include <iostream>
  2. include <typeinfo>

using namespace std; class BaseClass {

 virtual void f() {}; // make BaseClass polymorphic
 

}; class Derived1: public BaseClass {

}; class Derived2: public BaseClass {

}; int main() {

 int i;
 BaseClass *p, baseob;
 Derived1 object1;
 Derived2 object2;
 cout << "Typeid of i is ";
 cout << typeid(i).name() << endl;
 
 p = &baseob;
 cout << "p is pointing to an object of type ";
 cout << typeid(*p).name() << endl;
 p = &object1;
 cout << "p is pointing to an object of type ";
 cout << typeid(*p).name() << endl;
 p = &object2;
 cout << "p is pointing to an object of type ";
 cout << typeid(*p).name() << endl;
 return 0;

}


 </source>


An example that uses typeid on a polymorphic class hierarchy

<source lang="cpp">

  1. include <iostream>
  2. include <typeinfo>

using namespace std; class Mammal { public:

 virtual bool laysEggs() { 
   return false; 
 }

}; class Cat: public Mammal { public: }; class Platypus: public Mammal { public:

 bool laysEggs() { 
   return true; 
 }

}; int main() {

 Mammal *p, AnyMammal;
 Cat cat;
 Platypus platypus;
 p = &AnyMammal;
 cout << "p is pointing to an object of type ";
 cout << typeid(*p).name() << endl;
 p = &cat;
 cout << "p is pointing to an object of type ";
 cout << typeid(*p).name() << endl;
 p = &platypus;
 cout << "p is pointing to an object of type ";
 cout << typeid(*p).name() << endl;
 return 0;

}


 </source>


A simple example that uses typeid.

<source lang="cpp">

  1. include <iostream>
  2. include <typeinfo>

using namespace std;

class myclass1 {};

class myclass2 {};

int main(){

 int i, j;
 float f;
 char *p;
 myclass1 ob1;
 myclass2 ob2;
  
 cout << "The type of i is: " << typeid(i).name();
 cout << endl;
 cout << "The type of f is: " << typeid(f).name();
 cout << endl;
 cout << "The type of p is: " << typeid(p).name();
 cout << endl;
  
 cout << "The type of ob1 is: " << typeid(ob1).name();
 cout << endl;
 cout << "The type of ob2 is: " << typeid(ob2).name();
 cout << "\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";
  
 if(typeid(ob1) != typeid(ob2))
   cout << "ob1 and ob2 are of differing types\n";
  
 return 0;

}


 </source>


Demonstrate == and != relative to typeid.

<source lang="cpp">

  1. include <iostream>
  2. include <typeinfo>

using namespace std; class X {

 virtual void f() {}

}; class Y {

 virtual void f() {}

}; int main() {

 X x1, x2;
 Y y1;
 if(typeid(x1) == typeid(x2))
   cout << "x1 and x2 are same types\n";
 else
   cout << "x1 and x2 are different types\n";
 if(typeid(x1) != typeid(y1))
   cout << "x1 and y1 are different types\n";
 else
   cout << "x1 and y1 are same types\n";
 return 0;

}


 </source>


Demonstrate runtime type id.

<source lang="cpp">

  1. include <iostream>
  2. include <cstdlib>

using namespace std; class two_d_shape { protected:

 double x, y;

public:

 two_d_shape(double i, double j) {
   x = i;
   y = j;
 }
 double getx() { return x; }
 double gety() { return y; }
 virtual double area() = 0;

}; // Create a subclass of two_d_shape for triangles. class triangle : public two_d_shape {

 public:
   triangle(double i, double j) : two_d_shape(i, j) { }
   double area() {
     return x * 0.5 * y;
   }

}; // Create a subclass of two_d_shape for rectangles. class rectangle : public two_d_shape {

 public:
   rectangle(double i, double j) : two_d_shape(i, j) { }
   double area() {
     return x * y;
   }

}; // Create a subclass of two_d_shape for circles. class circle : public two_d_shape {

 public:
   circle(double i, double j=0) : two_d_shape(i, j) { }
   double area() {
     return 3.14 * x * x;
   }

}; // A factory for objects derived from two_d_shape. two_d_shape *factory() {

 static double i = (rand() % 100) / 3.0, j = (rand() % 100) / 3.0;
 i += rand() % 10;
 j += rand() % 12;
 cout << "Generating object.\n";
 switch(rand() % 3 ) {
   case 0: return new circle(i);
   case 1: return new triangle(i, j);
   case 2: return new rectangle(i, j);

}

 return 0;

} // Compare two shapes for equality. This means that their // types and dimensions must be the same. bool sameshape(two_d_shape *alpha, two_d_shape *beta) {

 cout << "Comparing a " << typeid(*alpha).name()
      << " object to a " << typeid(*beta).name()
      << " object\n";
 if(typeid(*alpha) != typeid(*beta)) return false;
 if(alpha->getx() != beta->getx() &&
    alpha->gety() != beta->gety()) return false;
 return true;

} int main() {

 // Create a base class pointer to two_d_shape.
 two_d_shape *p;
 // Generate two_d_shape objects.
 for(int i=0; i < 6; i++) {
   // Generate an object.
   p = factory();
   // Display the name of the object.
   cout << "Object is " << typeid(*p).name() << endl;
   // Display its area.
   cout << "    Area is " << p->area() << endl;
   // Keep a count of the object types that have been generated.
   if(typeid(*p) == typeid(triangle))
     cout << "    Base is " << p->getx() << " Height is "
          << p->gety() << endl;
   else if(typeid(*p) == typeid(rectangle))
     cout << "    Length is " << p->getx() << " Height is "
          << p->gety() << endl;
   else if(typeid(*p) == typeid(circle))
     cout << "    Diameter is " << p->getx() << endl;
   cout << endl;
 }
 cout << endl;
 // Make some objects to compare.
 triangle t(2, 3);
 triangle t2(2, 3);
 triangle t3(3, 2);
 rectangle r(2, 3);
 // Compare two two_d_objects.
 if(sameshape(&t, &t2))
   cout << "t and t2 are the same.\n";
 if(!sameshape(&t, &t3))
   cout << "t and t3 differ.\n";
 if(!sameshape(&t, &r))
   cout << "t and r differ.\n";
 cout << endl;
 return 0;

}


 </source>


Demonstrating run-time type id.

<source lang="cpp">

  1. include <iostream>

using namespace std;

class Mammal { public:

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

};

class Cat: public Mammal { public: };

class Platypus: public Mammal { public:

 bool lays_eggs() { return true; }

};

class Dog: public Mammal { public: };

// A factory for objects derived from Mammal. Mammal *factory(){

 switch(rand() % 3 ) {
   case 0: return new Dog;
   case 1: return new Cat;
   case 2: return new Platypus;
 }
 return 0;

}

int main(){

 Mammal *ptr; // pointer to base class
 int i;
 int c=0, d=0, p=0;
  
 // generate and count objects
 for(i=0; i<10; i++) {
   ptr = factory(); // generate an object
  
   cout << "Object is " << typeid(*ptr).name();
   cout << endl;
  
   // count it
   if(typeid(*ptr) == typeid(Dog)) d++;
   if(typeid(*ptr) == typeid(Cat)) c++;
   if(typeid(*ptr) == typeid(Platypus)) p++;
 }
  
 cout << "  Dogs: " << d << endl;
 cout << "  Cats: " << c << endl;
 cout << "  Platypuses: " << p << endl;
  
 return 0;

}


 </source>


Here is a simple example that uses typeid

<source lang="cpp">

  1. include <iostream>
  2. include <typeinfo>

using namespace std; class myclass1 { }; class myclass2 { }; int main() {

 int i, j;
 float f;
 char *p;
 myclass1 ob1;
 myclass2 ob2;
 cout << "The type of i is: " << typeid(i).name();
 cout << endl;
 cout << "The type of f is: " << typeid(f).name();
 cout << endl;
 cout << "The type of p is: " << typeid(p).name();
 cout << endl;
 cout << "The type of ob1 is: " << typeid(ob1).name();
 cout << endl;
 cout << "The type of ob2 is: " << typeid(ob2).name();
 cout << "\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";
 if(typeid(ob1) != typeid(ob2))
   cout << "ob1 and ob2 are of differing types\n";
 return 0;

}


 </source>


typeid Can Be Applied to Template Classes

<source lang="cpp"> // Using typeid with templates.

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


typeid for polymorphic class

<source lang="cpp">

  1. include <iostream>
  2. include <typeinfo>
  3. include <exception>

using namespace std; class CBase {virtual void f(){} }; class CDerived : public CBase {}; int main () {

 try {
   CBase* a = new CBase;
   CBase* b = new CDerived;
   cout << "a is: " << typeid(a).name() << "\n";
   cout << "b is: " << typeid(b).name() << "\n";
   cout << "*a is: " << typeid(*a).name() << "\n";
   cout << "*b is: " << typeid(*b).name() << "\n";
 } catch (exception& e) { cout << "Exception: " << e.what() << endl; }
 return 0;

}


 </source>


Use a reference with typeid.

<source lang="cpp">

  1. include <iostream>
  2. include <typeinfo>

using namespace std;

class Mammal { public:

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

};

class Cat: public Mammal { public: };

class Platypus: public Mammal { public:

 bool lays_eggs() { return true; }

};

// Demonstrate typeid with a reference parameter. void WhatMammal(Mammal &ob) {

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

}

int main() {

 Mammal AnyMammal;
 Cat cat;
 Platypus platypus;
  
 WhatMammal(AnyMammal);
 WhatMammal(cat);
 WhatMammal(platypus);
  
 return 0;

}


 </source>