-
Inheritance Troubles
I have two classes, one of which is derived from the other.
Code:
class CPrimitive
{
public:
CPrimitive();
~CPrimitive();
void Function1() { Function2() };
int Get_I() { return i; };
protected:
Function2() { i=1; };
int i;
}
class CBox : public CPrimitive
{
public:
CBox();
~CBox();
protected:
Function2() { i=2; };
}
//The inline functions are actualy declared in a .cpp file and are much
//more complex, but this example still stands.
//In my main code i do something like this
CBox box = new CBox();
box->Function1();
int what_is_I =box->Get_I();
I want Function1 to call the new definition of Function2 in the CBox class and have i=2 returned. However it calls the old Function2 from CPrimitive and returns i=1. Its there any way to get the code to reference the new Definition without redefining Function1 for CBox?
-
Change Function2 to a virtual function.