Reputation: 9363
As the title asks, are they any g++ builtin function (or any method) to know the class name from a class object pointer? For instance,
class Base {
};
class A : public Base {
};
class B : public Base {
};
void some_func(Base *p) {
printf("the name of class: %s", __some_g++_builtin_get_class_name(p) );
}
Similar to __PRETTY_FUNCTION__ in gcc builtin, I hope there would be a builtin function for g++ that help us analyze source code :)
Upvotes: 0
Views: 541
Reputation: 6675
You may be able to use typeid for this.
#include <cstdio>
#include <typeinfo>
class Base {
virtual int foo() { return 0; }
};
class A : public Base {
};
class B : public Base {
};
void some_func(Base *p) {
printf("the name of class: %s\n", typeid(*p).name() );
}
int main() {
some_func(new A);
}
Unfortunately it will probably not do what you want unless your base class has at least one virtual function (otherwise the type information may not be available).
Unlike __PRETTY_FUNCTION__
, the value you are looking for cannot be computed at compile time.
Upvotes: 1