Mega Code Archive

 
Categories / C++ Tutorial / Development
 

Use typeid

#include <iostream>  #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;  } 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