I have following example. Notice this is just example to illustrate the problem. The real situation is much complex.
The problem is that I must overload function from base class. If object used in calculate is type base it uses variable x which is common both for base and derived class. If object is of class derived it uses its specific variable y. So the real problem is how to define derived class before class base is defined.
I know tow workarounds of the problem, but none of them is a solution:
- make class derived basic class and loose benefits of inheritance
- change function calculate to
calculate(double y) {return x+y;)what means like solution but is not due to problem is much complex. Simply I need access to the rest of the object.
class base {
protected:
double x;
public:
double calculate(base b) {return x+b.x;}
double calculate(derived d) {return x+d.y;}
}
class derived: base {
public:
double y;
Is this problem resolvable?
thanks in advance...