The next example illustrates the use of C++ multiple inheritance. There are two disadvantages to using multiple inheritance with OLE. First, it is not possible to have an interface-level reference count. For more information about reference counting, see Chapter 2, "The Component Object Model." Second, there is the potential for confusion over the interpretation of the class statement. A standard C++ multiple inheritance declaration implies the "is a" relationship where an object inherits implementations. In OLE, however, interfaces are attributes of the object and implementations are not inherited.
The main advantage to using multiple inheritance lies in its simplicity. Only the prototypes for each of the interface methods are listed; no interface data structures or class definitions are necessary.
Because both InterfaceA and InterfaceB inherit from IUnknown, it is not necessary to list IUnknown explicitly in the class statement. A single implementation of the IUnknown methods (QueryInterface, AddRef, and Release) is sufficient.
class CObj : public InterfaceA, public InterfaceB
{
private:
ULONG m_ObjRefCount;
LPSTORAGE m_pStg;
LPOLEOBJECT m_pOleObj;
CDOC * m_pCDoc;
public:
CObj();
~CObj();
HRESULT QueryInterface(REFIID riid, LPVOID * ppvObj)
ULONG AddRef(void) { return ++m_ObjRefCount; }
ULONG Release(void);
HRESULT MethodA1(LPVOID * ppvObj);
HRESULT MethodA2(DWORD dwArg);
HRESULT MethodB1(void);
HRESULT MethodB2(DWORD dwArg1, DWORD dwArg2);
};