COM Aggregation   «Prev 

Supporting explicit interface navigation into aggregated objects

Explicit aggregation has the outer object coded to look for known interfaces implemented within aggregated objects.
The following pseudo-code makes the following assumptions:
  1. The outer object has aggregated two inner objects: IO1 and IO2.
  2. IO1 implements interface IX1; IO2 implements interface IX2.
  3. The outer object implements interface IX0.
  4. The outer object has nondelegating IUnknown interface pointers into each aggregated object in member variables m_pIUnknown_IO1 and m_pIUnknown_IO2.

class COuterCOMObj : public IX0 {
 IUnknown *m_pIUnknown_IO1;
 /* Assume this holds the non-delegating IUnknown for IO1 */
 IUnknown *m_pIUnknown_IO2;
 /* Assume this holds the non-delegating IUnknown for IO2 */
 HRESULT QueryInterface(REFIID riid, VOID **ppv) {
   /* See if the caller is asking for interface IX0 which is 
   implemented in the outer object */
   if (riid == IID_IX0) { 
     *ppv = (IX0 *) this;
   }
   /* See if the caller wants IX1 which is implemented 
   in aggregated object IO1 */
   else if (riid == IID_IX1) {
     //If so - call the non-delegating IUnknown in aggregated object IO1
      return m_pIUnknown_IO1->QueryInterface(riid, ppv);
    }
    //See if the caller wants IX2 which is implemented in aggregated object IO2
    else if (riid == IID_IX2) {
      //If so - call the non-delegating IUnknown in aggregated object IO2
      return m_pIUnknown_IO2->QueryInterface(riid, ppv);
    }
    else 
      return E_NOINTERFACE;      
     ((*IUnknown ) ppv)->AddRef();
     return S_OK;
  }
  ...
};