views:

116

answers:

2

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

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

How long will the string pointed to by the returned const char* pointer available to my program?

+10  A: 

As long as the class with rtti exists. So if you deal with single executable - forever. But for classes in a Dynamic Link Librariy it shifts a little. Potentially you can unload it.

Dewfy
+1 Most accurate answer, though you need to fix that first sentence, I know what you're trying to say, but it just seems like gibberish
Grant Peters
+3  A: 

The memory returned by type_info::name() will be available for the application's lifetime.

sbi
Is this guaranteed by the Standard? I know that the result of `typeid()` has application lifetime but I can't find anything that places a lifetime on the result of `type_info::name()`.
D.Shawley
TTBOMK, it is with the caveat given by Dewfy. However, since the standard doesn't even acknowledge the existence of dynamic libraries...
sbi