How to determine in runtime the class type of inhetited class from common base class?
-
In scenario like this, I've putted pseudo function GetRTTI(). How can I detect in the runtime the actual(derived) class type? class the_base { }; class A : public the_base { }; class B : public the_base { }; class C : public the_base { }; ... the_base* pList[] = { new A(), new B(), new C()); C* pTarget = nullptr; if(pList[2]->GetRTTI("C"))//detecting the actual type { pTarget = dynamic_cast< C* >(pList[2]);//casting to the type } //using pTarget ...
-
In scenario like this, I've putted pseudo function GetRTTI(). How can I detect in the runtime the actual(derived) class type? class the_base { }; class A : public the_base { }; class B : public the_base { }; class C : public the_base { }; ... the_base* pList[] = { new A(), new B(), new C()); C* pTarget = nullptr; if(pList[2]->GetRTTI("C"))//detecting the actual type { pTarget = dynamic_cast< C* >(pList[2]);//casting to the type } //using pTarget ...
The class type or the class name? Is typeid() of any help?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
The class type or the class name? Is typeid() of any help?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
In scenario like this, I've putted pseudo function GetRTTI(). How can I detect in the runtime the actual(derived) class type? class the_base { }; class A : public the_base { }; class B : public the_base { }; class C : public the_base { }; ... the_base* pList[] = { new A(), new B(), new C()); C* pTarget = nullptr; if(pList[2]->GetRTTI("C"))//detecting the actual type { pTarget = dynamic_cast< C* >(pList[2]);//casting to the type } //using pTarget ...
I think that at runtime, you will have to use the (slow) dynamic_cast or have the classes know their types via a ID of some sort.
Nihil obstat