You can use typeid
to get the name of any type. Although it will vary from compiler to compiler, and may not be a pretty name.
#include <iostream>
#include <typeinfo>
struct MyStruct { };
int main()
{
std::cout << typeid(MyStruct).name() << std::endl;
}
If you need to do this for a lot of classes, you could make the call part of a base class, then any class that needs the functionality can just inherit from it.
#include <iostream>
#include <typeinfo>
class NamedClass
{
public:
virtual ~NamedClass() { }
std::string getNameAsString()
{
return typeid(*this).name();
}
};
class MyStruct : public NamedClass
{
};
int main()
{
MyStruct ms;
std::cout << ms.getNameAsString() << std::endl;
}