www.digitalmars.com         C & C++   DMDScript  

c++ - help with a 'diamond' situation


I try to understand the decoupled design based on interfaces but I see that 
some problems arise and I have not found a place where this is discussed.
The client code works with interface classes, decoupled from their 

class IItem
    virtual ~IItem() { }
    virtual bool get_name(string& name) = 0;                     // some 
common properties
    virtual bool get_size(unsigned long &size) = 0;

class IAItem : public IItem
        // methods specific to IAItem

class IBItem : public IItem
        // methods specific to IBItem

// client code uses
IItem* pItem = ...
string name;
usigned long size;

The implementation will want to have a common abstract class derived from 
IItem where these common properties are implemented:

class ItemAbstract : public IItem
    virtual bool get_name(string& name) { ... }                    // some 
common properties implemented
    virtual bool get_size(unsigned long &size) { .... }

but the problem appears with the implementation of the IAItem or IBItem:

class AItem : public IAItem, public ItemAbstract
    // implementation of IAItem

           IAItem              ItemAbstract

this implementation is ambigous because the AItem class will have 2 
instances of IItem (one inherited through IAItem and one through the 
ItemAbstract) so i need that:

class IAItem : virtual public IItem
class ItemAbstract : virtual public IItem

But, although this works, overseeing (for the moment) that fact that this 
will give an warning saying that AItem inherits the ItemAbstract methods 
through dominance, the problem i see is that at one moment I needed to 
change the initial design of the interface.
What if I could not do that, i.e. maybe those interfaces are from a library 
or something, hence read-only for me (the implementor) ?
It feels like the design is not OK but in the same way, since what's it's 
trying to achieve is so simple, I can't see another way.

What's your opinion ?

Oct 19 2007