Table of contents
1.
Introduction
2.
Typeid Operator in C++ 
2.1.
Basic Syntax of Typeiod Operator
2.2.
Parameters  (example all the parameters in the syntax)
2.3.
The return type of Typeid Operator
3.
Uses of Typeid
3.1.
When the operand is an expression
3.1.1.
Algorithm
3.1.2.
Output
3.1.3.
Explanation
3.2.
When the operand is a variable or an object
3.2.1.
Algorithm
3.2.2.
Output
3.2.3.
Explanation
4.
Frequently Asked Questions
4.1.
What is the Typeid operator in C++?
4.2.
Why Typeid () function is used?
4.3.
What is the difference between running time and time complexity?
4.4.
Why is C++ known as an Object-Oriented Programming Language?
4.5.
Why has C++ considered the backbone of OS and Browsers?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Typeid Operator in C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C++ is one of the easiest and fastest programming languages. It is a general-purpose programming language having excellent library support. C++ is widely used in banking applications, embedded systems, games, and web browsers.

Typeid Operator in Cpp

As we know that operators are the symbol that tells the compiler to perform some specific mathematical or logical manipulations. Today we will discuss one such most used operators, typeid, in C++.

Also, see Literals in C, Fibonacci Series in C++  and,  Unary operator overloading in c++.

Typeid Operator in C++ 

A program can use the typeid operator to get the derived type of an object referred to by a reference or a pointerTypeid and dynamic cast operators in C++ offer runtime type identification (RTTI) support.

C++ img

The main purpose of using Typeiod Operator in C++ is-

  • Typeid is an operator in C++ provides a program with the ability to retrieve the derived type of the object. 
  • It is used when an object's dynamic type or runtime type information is required.
  • It is included in the <typeinfo> library. Therefore, this library needs to be included in the program.

Basic Syntax of Typeiod Operator

typeid(type);
OR
typeid(expression);

Parameters  (example all the parameters in the syntax)

The typeid operator accepts the following parameters:

  • type: This parameter is passed when a variable's or object's runtime type information is required.
  • expression: When the runtime type information of an expression is needed, the expression parameter is passed. 

The return type of Typeid Operator

The typeid operator in C++ returns the runtime type or the dynamic type information of an object or an expression. The data type information of the returned value as a reference to an object is of the type const type_info.

The lvalue of type const std::type info is also the result of the typeid operator. The type of expression expr is represented by this value. To utilize the typeid operator, you must include the default template library header <typeinfo>.

Uses of Typeid

As per the passed operand type, the typeid operator can be used in various ways as discussed below.

When the operand is an expression

Lets now implement the code for typeid operator in C++, when the operand is an expression.

Algorithm

  • In the first step, we have declared three variables, i.e.of int data type, of char data type and 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 got the expected result.
     
#include <iostream>
#include <typeinfo>
using namespace std;

int main()
{
	int i = 9;
           char c = 'x';
	double d = 11.22;
	
	// Get the type info using typeid operator
	const type_info& t1 = typeid(i * d);
	const type_info& t2 = typeid(i * c);
	const type_info& t3 = typeid(c);

	// Printing the types of t1, t2 and t3
	cout << "t1 is of type "
		<< t1.name() << endl;

	cout << "t2 is of type "
		<< t2.name() << endl;

	cout << "t3 is of type "
		<< t3.name() << endl;

	return 0;
}

Output

Output1

You can try by yourself with the help of online c++ compiler.

Explanation

Type of t1 is d which means that t1 is of double data type because here d indicates double.

Type of t2 is i which means that t2 is of int data type because here t2 indicates integer.

Type of t3 is c which means that t3 is of char data type because t3 indicates character.

When the operand is a variable or an object

Lets now implement the code for typeid operator in C++, when the operand is a variable or an object.

Algorithm

  • In the first step, we have declared three variables, i.e.andof int data type and of char data type.
  • Then we assigned the type_info of all the variables using the C++ typeid() operator.
  • Then we compared the types of all the variables and got the expected result.
     
#include <iostream>
#include <typeinfo>
using namespace std;

int main()
{
	int a, b;
	char c;

	// Get the type info using typeid operator
	const type_info& t1 = typeid(a);
	const type_info& t2 = typeid(b);
	const type_info& t3 = typeid(c);

	//  Here we will check if t1 and t2 types are similar
	if (t1 == t2)
		cout << "a and b are of the same type" << endl;
	else
		cout << "a and b are of different type" << endl;

	// Here we will check if t2 and t3 types are similar
	if (t2 == t3)
		cout << "a and c are of same type" << endl;
	else
		cout << "b and c are of different type" << endl;

	return 0;
}

Output

Output 2

Explanation

Type of a and b is of int data type so they are of same type.

Type of b and c is of int and char data type, respectively so they are of different type.

Must Read Lower Bound in C++

Frequently Asked Questions

What is the Typeid operator in C++?

A program can use the typeid operator to get the derived type of an object referred to by a reference or a pointer.

Why Typeid () function is used?

The typeid operator helps us determine an object's type at run time. 

What is the difference between running time and time complexity?

Time complexity is the theoretical concept about the asymptotic behavior of an algorithm but running time is the time taken by the program to execute, which depends on the programming language, compiler as well as processor.

Why is C++ known as an Object-Oriented Programming Language?

C++ is known as an Object-Oriented Programming Language as it views a problem or a task in terms of objects involved rather than the procedure to do it.

Why has C++ considered the backbone of OS and Browsers?

Almost every operating system, like Windows, Linux, and Mac OSX, is developed using C++. Many famous Browsers like Mozilla Firefox and Chrome have also been developed using C++. 

Conclusion

In this article, we have discussed the details of the typeid operator in C++ with examples.

We hope this blog has helped you enhance your knowledge of the typeid operator in C++.

We encourage you to check out our other articles to learn more about C++.

 

You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blogs to help other ninjas grow. Happy Coding!!

Live masterclass