I would like to be able to get the name of a class for each object as std::string
. As easy solution would be to define a virtual function get_name
for each class:
class Class1 {
std::string get_name () { return "Class1";}
}
This process is potentially error-prone, because it requires to copy and paste the name of the class for every class. Is there a better way to auto-generate this member method by using MACRO's or any other trick at compile time?
[UPDATE]:
I just searched the available answers and the best options are:
struct Class1 {
std::string get_name() {
return __PRETTY_FUNCTION__;
};
std::string get_name2() {
return typeid(*this).name();
};
};
The results need a bit of post processing:
std::__cxx11::string Class1::get_name()
4Class1