C++ – How to get C++ object name in run time


Can I get an object's name in run time (like getting an object's type via RTTI)? I want the object to be able to print its name.

Best Solution

Since objects in C++ don't have any names, you cannot get them. The only thing you can get to identify an object is its address.

Otherwise, you can implement your naming scheme (which means the objects would have some char* or std::string member with their name). You can inspire yourself in Qt with their QObject hierarchy, which uses a similar approach.