rtti - Print type of object referenced by base-class pointer in C++? -
if have class a, , class b subclasses a, should typeid(x)
give type of or type of b, if x given by:
a *x = new b();
in tests type of - not useful - i'm not sure if that's due how c++ works, or if due compiler settings?
example code:
#include <iostream> #include <typeinfo> // remember add virtual member function in // enable rtti. struct { virtual ~a() {} }; struct b : { virtual ~b() {}}; int main() { a* ap = new b(); std::cout << typeid(ap).name() << std::endl; std::cout << typeid(*ap).name() << std::endl; }
output, g++ 4.8.2:
p1a 1b
Comments
Post a Comment