H
hswerdfe
I have several Classes that derive from a common Base Class
Each of these Classes Has a Differnt Version of a Function with the Same
Short name, but different Long Name.
See Bellow:
// Base Class
class CBase
{
void DoStuff(CBase* other);
}
//Derived Class 1
class CDerived1: public CBase
{
void DoStuff(CDerived1* other);
}
// Derived Class 2
class CDerived1: public CBase
{
void DoStuff(CDerived1* other);
}
What I want to do is Create A new object of the Same Type that is being
passed into my function
then call the apropriate function
my code currently looks like this:
void OtherFunction(CBase* MyDerived)
{
CBase* myNewBase = (CBase*)
MyDerived->GetRuntimeClass()->CreateObject();
myNewBase->DoStuff(MyDerived);
}
but this always calls CBase:
oStuff(CBase* other);
and I want it to call CDerived1:
oStuff(CDerived1* other);
or one of the other derived functions
the only way I can think of doing this is to use a lot of
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived1)))
//cast then call
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived2)))
//cast then call
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived3)))
//cast then call
is there a better way to call the Derived Classes Member function?
thanks,
how
Each of these Classes Has a Differnt Version of a Function with the Same
Short name, but different Long Name.
See Bellow:
// Base Class
class CBase
{
void DoStuff(CBase* other);
}
//Derived Class 1
class CDerived1: public CBase
{
void DoStuff(CDerived1* other);
}
// Derived Class 2
class CDerived1: public CBase
{
void DoStuff(CDerived1* other);
}
What I want to do is Create A new object of the Same Type that is being
passed into my function
then call the apropriate function
my code currently looks like this:
void OtherFunction(CBase* MyDerived)
{
CBase* myNewBase = (CBase*)
MyDerived->GetRuntimeClass()->CreateObject();
myNewBase->DoStuff(MyDerived);
}
but this always calls CBase:
and I want it to call CDerived1:
or one of the other derived functions
the only way I can think of doing this is to use a lot of
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived1)))
//cast then call
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived2)))
//cast then call
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived3)))
//cast then call
is there a better way to call the Derived Classes Member function?
thanks,
how