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:


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 name()  raw_name() 


Share the Post: