Reputation: 327
What is the best way to get a 'shared_ptr' class name?
Lets say I have:
std::shared_ptr<Object> objPtr;
How can I get "Object" as a string?
Upvotes: 0
Views: 666
Reputation: 666
The name of the class, as a string, is generally not accessible at runtime. The easiest thing to do is simply define the class name as a const member field of the object, as one of the comments suggested.
However, I caution that it is very likely that program designs that require the string name of the class are a very bad idea, and encourage you to make better use of C++'s strongly-typed nature rather than checking if the name satifies certain conditions. Unless, of course, you simply want to name of the class for debug logging.
Upvotes: 1
Reputation: 249123
You can do it this way:
typeid(decltype(*objPtr)).name()
Note however that the name it returns may be "mangled." How this is done, and how to "demangle" the name, is platform-dependant (aka "implementation defined").
Upvotes: 3