0

The idea is for the users to create a new class and inherit only the base class and few special classes whose special functions they want.

class Base {
    Data data;
public:
    void init(Data d) data(d) {}
    // other basic functions
}

class SpecialX: public Base {
public:
    Result specialX() {
        Result r = compute(data); // uses Base::data to compute some result
        return r;
    }
}

class SpecialY: public Base {
public:
    Result specialY() {
        Result r = compute(data); // uses Base::data to compute some result
        return r;
    }
}

If user wants only init and specialX functions, they could do something like -

class My: public Base, public SpecialX {} // user doesn't need specialY function, so they do not inherit it

My m;
m.init(someData);
m.specialX(); // returns the result by computing specialX

But this throws the ambiguity error due to the Diamond problem in C++.

How do I structure my classes so that I can create as many special classes as I want, and let users inherit the ones that they need?

maverick
  • 119
  • 4

0 Answers0