1

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...

2
  • 2
    You should use virtual functions rather than overloading. You are looking for wrong solution. Commented Sep 20, 2019 at 6:45
  • A base class being aware of any of its derived classes is a strong indicator that you should redesign. Commented Sep 20, 2019 at 6:51

1 Answer 1

4

Forward declaring derived then defining the bodies of calculate out of line after the definition of derived should work:

class derived;

class base {
protected:
    double x;
public:
    double calculate(base b);
    double calculate(derived d);    
}

class derived: base {
public:
    double y;
};

inline double base::calculate(base b) {return x+b.x;}
inline double base::calculate(derived d) {return x+d.y;}

As others have said this could be a poor design with base requiring knowledge of derived. Maybe calculate would be better as a free function:

class base {
public:
    double x;    
}

class derived: base {
public:
    double y;
};

inline double calculate(base a, base b) {return a.x+b.x;}
inline double calculate(base a, derived b) {return a.x+b.y;}

I've made x public to simplify the example, you could make calculate a friend (which would again require knowledge of the derived class in base) or an an accessor function if you don't want x to be public.

A third more idomatic option would be to add a virtual method to get the required value for each class:

class base {
protected:
    double x;
    virtual double getValue() const { return x; }
public:
    double calculate(const base& b) { return x + b.getValue(); }
}

class derived: public base {
protected:
    double getValue() override const { return y; }     
private:
    double y;
};

Note that calculate must take the class by reference to avoid object slicing (and its more efficient).

Depending on your requirements calculate might be better implemented as:

double calculate(const base& b) { return getValue() + b.getValue(); }

This would mean that base.calculate(derived) would return the same result as derived.calculate(base).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.