devxlogo

Capturing a type_info Object

Capturing a type_info Object

The operator typeid returns a const type_info object associated with its argument. However, the returned object is a temporary one, so if you have to call several member functions from it, you will have to use the typeid expression repeatedly–a tedious and costly operation:

 cout<

Note that you cannot copy-construct or assign the returned type_info object either:

 type_info ti = typeid(myObj); //compilation error; copy constructor and operator= are private 

As a workaround, you can initialize a reference or a pointer with the temporary object.

 type_info *pti = & typeid (myObj) ;  // OK, a pointer with the address of the returned objectcout << pti->name()  << endl;cout << pti->raw_name() << endl; 
See also  How College Students Can Shape the Future of Tech Responsibility
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist