Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will multiple calls to typeid(T).name() return the same pointer?

In C++ I can use typeid operator to retrieve the name of any polymorphic class:

const char* name = typeid( CMyClass ).name();

The string pointed to by the returned const char* will be available to my program for as long as the corresponding class exists.

Will multiple calls of typeid(T).name() return the same pointer value for the same class T or are they allowed to return different pointers?

like image 553
sharptooth Avatar asked Apr 25 '11 13:04

sharptooth


2 Answers

No they are not required to return the same pointer.

I have also seen in the wild system that do not.
Each DLL had its own copy of the type object that contained the nmae thus calling the typeid(N).name in shared lib A and shared lib B will result in a different pointer (Though the same underlying string).

like image 61
Martin York Avatar answered Sep 19 '22 10:09

Martin York


Probably, but why rely on that rather than the actual type info if you want to do comparisons?

like image 22
John Zwinck Avatar answered Sep 21 '22 10:09

John Zwinck