Retrieving a c++ class name programmatically
I was wondering if it is possible in C++ to retrieve the name of a class in string form without having to hardcode it into a variable or a getter. I'm aware that none of that information is actually used at runtime, therefor it is unavailable, but are there any macros that can be made to create this functionality?
Edit: May be helpful to note that I'm actually trying to retrieve the name of a derived class, and I'm using Visual C++ 2008 Express Edition.
You can use typeid
:
#include <typeinfo>
std::cout << typeid(obj).name() << "\n";
However, the type name isn't standardided and may differ between different compilers (or even different versions of the same compiler), and it is generally not human readable because it is mangled.
On GCC and clang (with libstdc++ and libc++), you can demangle names using the __cxa_demangle
function (on MSVC demangling does not seem necessary):
#include <cxxabi.h>
#include <cstdlib>
#include <memory>
#include <string>
std::string demangle(char const* mangled) {
auto ptr = std::unique_ptr<char, decltype(& std::free)>{
abi::__cxa_demangle(mangled, nullptr, nullptr, nullptr),
std::free
};
return {ptr.get()};
}
This will still not necessarily be a readable name — for instance, std::string
is a type name for the actual type, and its complete type name in the current libstdc++ is std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
; by contrast, in the current libc++ it’s std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >
. “Prettifying” type aliases is unfortunately not trivial.
If you just want to check if it's certain class, then
typeid(obj) == typeid(CSubClass)
will always work regardless of the implementations.
Otherwise, a convenient way is to declare:
virtual const char* classname() { return "CMyClass";}
and implement per subclass.