1

My classes currently look like this:

GetAndSet{ 
    virtual int get() = 0;
    virtual void set() = 0;
}

WindowsGetAndSet : public GetAndSet{
    virtual int get();
    virtual void set();
}    

However, I've ran into some new constraints and I've realized that:

  • Sometimes I only need get methods
  • Whenever I use set methods I also need get methods

Thus, I'd like to do something like this:

Get{ 
    virtual int get() = 0;

}

Set : public Get{
    virtual void set() = 0;
}

WindowsGet : public Get{
    virtual int get();
}    

WindowsGetAndSet: public WindowsGet, public Set{
    // inherits the get methods from WindowsGet
    virtual void set();
}

This causes me to run into the Diamond Problem where WindowsGetAndSet is inheriting Get twice, once from Set and once from WindowsGet.

Can I simply have Set and WindowsGet use virtual inheritence to inherit Get, or do I need to think up a new design all together?

TrolliOlli
  • 909
  • 2
  • 10
  • 18
  • It depends on what you need for your design to work. Do you need different Get functions, one for WindowsGet and one for Set? Or do you want a single function? Virtual inheritance works on the base class by guaranteeing that there exists only one object of that base class. – Okkenator Jun 09 '15 at 13:43
  • Consider the cost and benefit of defining Get and Set and their respective responsibilities. Just because you can abstract to this level, doesn't mean you should. What design benefit does this abstraction yield? – Okkenator Jun 09 '15 at 13:43

0 Answers0