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.
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.
This may be GCC-specific:
#include <typeinfo>
#include <iostream>
template <typename T>
void foo(T t)
{
std::cout << typeid(t).name() << std::endl;
}