all the while, I am using dynamic_cast to determine Parent-Child relationship of an object.
#include <iostream>
class A {
public:
virtual ~A() {}
};
class B : public A {
};
class C : public A {
};
int main()
{
B b;
std::cout<< typeid(b).name()<< std::endl; // class B
A* a = dynamic_cast<A *>(&b);
if (a) {
std::cout<< "is child of A"<< std::endl; // printed
}
C* c = dynamic_cast<C *>(&b);
if (c) {
std::cout<< "is child of C"<< std::endl; // Not printed
}
getchar();
}
May I know is it possible that I can determine parent-child relationship, of an object, through typeid? For example, how I can know B is the child of A, by using typeid checking?
Thanks.
I don’t think you can do that in current C++ using the information in
typeinfoonly. I know ofboost::is_base_of(Type Traits will be part of C++0x):