Let's say i have a base class Base
, and two derived classA,B
How can i dynamically create a obj
, without knowing if its ClassA
or ClassB
, i need to achieve such things like that:
func(Base &b)
{
X *obj = new X();
}
X would be A or B, depends on the actual Type of b.
The only way i can think of so far is to check if dynamic_cast<A&>(b)
succeed or not, if an error is thrown, it must be a ClassB.
Is there a better way than that? because i may have more derived classes.ClassA,B,C,D...