If my classes defined as follow:
Code:
class A
{...};
class B : public A
{...};
class C : public A
{...};
and someone instances an object B from A pointer
Now, how can I instance another object same type as *ap when I get ap pointer? Of course, I dont know whether *ap is an B object or C object.
Maybe I can define an Identify() function in classes to indentify themself, eg:
Code:
class B{
public:
vitual int Indentify(void)
{
cout<<"I am B";
return 1;
}
....
};
class C{
public:
vitual int Indentify(void)
{
cout<<"I am C";
return 2;
}
....
};
void main(void)
{
...
A* ap2;
switch(ap->Identify())
{
case 1:
ap2=new B;
break;
case 2:
ap2=new C;
}
}
How can I impliment this without using "switch"?
Thank you