You can just define virtual destructor in Base class:
#include <iostream>
#include <malloc.h>
#include <cxxabi.h>
using namespace std;
class Base {
public:
virtual ~Base() {}
std::string getClassName() {
char *str = (char*) malloc(1024);
size_t size = 1024;
int status;
char *res = abi::__cxa_demangle( typeid(*this).name(), str, &size, &status );
return std::string(res);
}
};
class A : public Base { /*...*/ };
class B : public A { /*...*/ };
int main()
{
A a;
B b;
cout << a.getClassName() << endl;
cout << b.getClassName() << endl;
}
Output:
./test
A
B
BTW in this case it's better let __cxa_demangle to allocate memory (and don't forget to ::free() it because this example code has memory leak)