C++ typeid

Introduction
The typeid is an operator in C++ used to know an object's runtime or dynamic type. The typeid operator is in the typeinfo library header in C++. So, to use the typeid operator, we must include the typeinfo library header.
Syntax
OR
Parameters
The C++ typeid operator accepts a single parameter, and this parameter is based on the syntax that is used in the program.
-
Type: The type parameter is passed inside the typeid operator when we require information about the runtime type of a variable or an object. We do not need to evaluate anything or perform any pre-computation in this parameter. We pass the type parameter, and the information about the runtime type is extracted.
-
Expression: The expression parameter is passed inside the typeid operator when we require information about the runtime type of an expression. In this parameter, we need to first evaluate the expression, and then the information about the runtime type is extracted.
Return Value
The C++ typeid operator returns the runtime or dynamic type information of an expression or object. The data type of the returned value is const type_info.
What are the Uses of C++ Typeid Operator?
According to the passed operand type, the typeid operator can be used differently, as shown below.
When the Operand is a Variable of the Object
In this code, we will see how we can get information about the type of the parameter when the variable is passed as a parameter.
C++ Implementation
Output
Explanation
- In the first step, we have declared four variables, i.e., a of int data type, b of char data type, c of float data type, and d of double data type.
- Then we assigned the type_info of all the variables using the C++ typeid() operator.
- Then, we checked the types of all the variables and accordingly produced the following output
- Type of a is i, which means that a is of int data type because i indicates int.
- Type of b is c, which means that b is of char data type because c indicates char.
- Type of c is f, which means that c is of float data type because f indicates float.
- Type of d is d, which means that d is of double data type because d indicates double.
When the Operand is an Expression
In this code, we will see how we can get information about the type of parameter when the expression is passed as a parameter.
C++ Implementation
Output
Explanation
- In the first step, we have declared four variables, i.e., a of int data type, b of char data type, c of float data type, and d of double data type, and initialized them with the values of 5, 'x', 1.5, and 3.5095.
- Then we assign the type_info of some expressions using the C++ typeid() operator.
- Then, we checked the types of all the variables and accordingly produced the following output
- **Expression a+b : i, which means that a+b is of int data type because i indicates int.
- **Expression a * c : f, which means that a * c is of float data type because f indicates float.
- **Expression d * c : d, which means that d * c is of double data type because d indicates double.
- **Expression a+d : d, which means that a+d is of double data type because d indicates double.
Examples of C++ typeid Operator
Example 1
To get information about the type of simple variables:
C++ Implementation
Output
Example 2
To get information about the type of both polymorphic and non-polymorphic classes:
C++ Implementation
Output
Example 3
Getting information about the type of expression
C++ Implementation
Output
Learn More
Conclusion
In this quick tutorial, we have discussed the typeid() operator in C++. We can extract the following conclusions from the article.
- The typeid operator is present in the typeinfo library header in C++. So, to use the typeid operator, we must include the typeinfo library header.
- The typeid operator can be used to get information about the variables and expressions.
- The C++ typeid operator returns the runtime or dynamic type information of an expression or object.