E
ectoplasm
Abstract base class A. Derived from A are class B and C, both concrete.
I hold a pointer to an object of class A, an instance which is either B
or C.
I have trouble with making the following work, while I think it should
be possible:
A *pA = new B; // One of...
A *pA = new C; // ...these two
DoSomething(pA); // Call overloaded method
This called method is overloaded, and looks like this:
void DoSomething(B *pB)
{
// Code to handle a B
}
void DoSomething(C *pC)
{
// Code to handle a C)
}
If this is not possible, what should I do? Is the following the only
way?
void DoSomething(A *pA)
{
if (pA->IsTypeOf(B))
// Code to handle a B
else if (pA->IsTypeOf(C))
// Code to handle a C
}
There must be something more "OO".
Thanks,
E.
I hold a pointer to an object of class A, an instance which is either B
or C.
I have trouble with making the following work, while I think it should
be possible:
A *pA = new B; // One of...
A *pA = new C; // ...these two
DoSomething(pA); // Call overloaded method
This called method is overloaded, and looks like this:
void DoSomething(B *pB)
{
// Code to handle a B
}
void DoSomething(C *pC)
{
// Code to handle a C)
}
If this is not possible, what should I do? Is the following the only
way?
void DoSomething(A *pA)
{
if (pA->IsTypeOf(B))
// Code to handle a B
else if (pA->IsTypeOf(C))
// Code to handle a C
}
There must be something more "OO".
Thanks,
E.