hi, this is a little complicated to explain, so i'm just going to do the best i can.
To summarize my problem, i have a base class with a virtual function (which is overridden in a derived class).
I need a function in the base class to call the correct version of the virtual function. (in this case, the one in the derived class)
here's a basic example:
Code:
class Base
{
public:
~Base();
static Derived *child;
void callRelease();
virtual void Release(int value);
}
class Derived : public Base
{
public:
virtual void Release(int value);
}
/////////////// Base Member Functions ////////////////////
Base::~Base()
{
callRelease();
}
void Base::callRelease()
{
child->Release(3); //PROBLEM: I need this to call the Derived::Release(3)!!
}
void Base::Release(int value)
{
//basic release stuff
}
////////////Derived Member Function/////////////////
void Derived::Release(int value)
{
//more advanced, Derived-specific, release stuff
}
/////////////Main Function///////////////
Derived *Base::child; //initialize the static variable,
//(points to derived)
void main()
{
Derived *d = new Derived();
Base::child = d; //the child static variable now points to d
delete d;
}
Heres what happens:
- d is created and instanciated with a new Derived class;
- d is deleted, first calling the empty Derived destructor
- d then calls the Base destructor, which calls CallRelease();
Heres where the problem is:
at this point, I need CallRelease to call the specific version of release, Derived::Release(int value).
I assumed that because the static pointer was to a derived class,
child->Release(3) would call Derived::Release();
Instead, it calls Base::Release(3), and i can't figure out why, or how to fix it.
sorry that may have been hard to follow, and thanks for the help in advance.
it's probably something stupid (as it always is)
but how do i fix it?!