typeid Operator - Type Casting

How is typeid operator used in C++?

Explanation

typeid operator is a type casting method which is used to check the type of an expression. In C++, the typeid keyword is used to determine the class of an object at runtime

Example :


#include <iostream.h>
#include <typeinfo.h>
int main()
{
int a;
char *c;
cout << "The type of a is: " << typeid(a).name() << endl;
cout << "The type of c is: " << typeid(c).name() << endl;
return 0;
}

Result :

The type of a is: int
The type of c is: char *

In the above example the "typeid" is used to find the data type.

C++ Tutorial


Ask Questions

Ask Question