class IA : public IUnknown { virtual HRESULT __stdcall A1() = 0; }; class IB : public IUnknown { virtual HRESULT __stdcall B1() = 0; }; class INonDelegatingIUnknown { virtual HRESULT __stdcall objQueryInterface(REFIID riid, VOID **ppv) = 0; virtual ULONG __stdcall objAddRef() = 0; virtual ULONG __stdcall objRelease() = 0; }; class CCOMObj : INonDelegatingIUnknown, IA, IB { private: ULONG m_refcnt; IUnknown *m_pIOuterIUnknown; public: //INonDelegatingIUnknown methods HRESULT objQueryInterface(REFIID riid, VOID **ppv) { ... } ULONG objAddRef() { ... }; ULONG objRelease() { ... } //IUnknown methods for IA and IB HRESULT QueryInterface(REFIID riid, VOID **ppv) { return m_pIOuterIUnknown->QueryInterface(riid, ppv); } ULONG AddRef() { return ++m_refcnt; } ULONG Release() { if (--m_refcnt == 0) { delete this; return 0; } return m_refcnt; } //IA methods HRESULT A1() { ... } //IB methods HRESULT B1() { ... } /*Assume that the outer object's IUnknown pointer is passed into the constructor */ CCOMObj(IUnknown *pOuter) : m_refcnt(0),m_pIOuterIUnknown(NULL) { m_pIOuterIUnknown = pOuter; } };