typeid operator in C++ with Examples
typeid is an operator in C++.
- It is used where the dynamic type or runtime type information of an object is needed.
- It is included in the <typeinfo> library. Hence inorder to use typeid, this library should be included in the program.
- The typeid expression is an lvalue expression.
typeid(type); OR typeid(expression);
Parameters: typeid operator accepts a parameter, based on the syntax used in the program:
- type: This parameter is passed when the runtime type information of a variable or an object is needed. In this, there is no evaluation that needs to be done inside type and simply the type information is to be known.
- expression: This parameter is passed when the runtime type information of an expression is needed. In this, the expression is first evaluated. Then the type information of the final result is then provided.
Return value: This operator provides the runtime type information of the specified parameter and hence that type information is returned, as a reference to an object of class type_info.
Usage: typeid() operator is used in different way according to the operand type.
- When operand is a variable or an object.
i and j are of simiar type j and c are of different type
- When operand is an expression.
ti1 is of type f ti2 is of type i ti3 is of type c