描述:
我最近看Dale Rogerson写的Inside COM一书,在第八章讲aggregation的时候,有一个例子。
该例子包含一个客户端client,二个组件component(class CA in CMPNT1.CPP and class CB in CMPNT2.CPP).
interface IX : IUnknown
{
virtual void __stdcall Fx() = 0 ;
};
interface IY : IUnknown
{
virtual void __stdcall Fy() = 0 ;
};
struct INondelegatingUnknown
{
virtual HRESULT __stdcall
NondelegatingQueryInterface(const IID&, void**) = 0 ;
virtual ULONG __stdcall NondelegatingAddRef() = 0 ;
virtual ULONG __stdcall NondelegatingRelease() = 0 ;
} ;
class CA : public IX
class CB : public IY,
public INondelegatingUnknown
在创建外部组件和内部组件过程中,CA的成员变量IUnknown* m_pUnknownInner被赋以static_cast<INondelegatingUnknown*>(this) ;this是CB的this指针pointer。
我的问题是:
当在客户端中调用pIX->QueryInterface(IID_IY, (void**)&pIY)
该函数会进一步调用CA::QueryInterface内的m_pUnknownInner->QueryInterface(iid,ppv) ;
该函数为什么会调用CB::NondelegatingQueryInterface函数呢?
这两个函数名称并不一样啊?
编译器在背后做了什么呢?