Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Advanced
Dec 8, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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<<typeid(myObj).name() <<endl;
cout<<typeid(myObj).raw_name() <<endl; 
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 object
cout << pti->name()  << endl;
cout << pti->raw_name() << endl; 
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date