[英]Why do IUnknown* pointers retrieved through different interfaces of the same COM object have the same value?
我有以下COM接口層次結構和一個實現它們的類:
interface IX : public IUnknown{};
interface IY : public IUnknown{};
class CA: public IX, public IY{};
這里的class CA
有效地繼承了IUnknown
兩次。
我們知道class CA
有兩個vtable指針 - 一個指向IX
,另一個指向IY
。 這樣IUnknown
存儲在IX
子對象是不同IUnknown
存儲在IY
子對象。
然而,當我們在同一個對象上調用IX::QueryInterface()
或IY::QueryInterface()
並查詢IUnknown
我們得到相同的IUnknown*
指針。
為什么會這樣?
這就是所謂的“對象標識”要求 ,它規定每當你從兩個對象請求IUnknown
,如果那些是不同的對象,則獲得不同的指針,如果是同一個對象,則獲得相同的指針。
每個QueryInterface()
實現都必須滿足此要求。 這通常是通過選擇IUnknown
哪一個返回並堅持下去來完成的 :
HRESULT CA::QueryInterface( REFIID iid, void** ppv )
{
if( iid == __uuidof( IUnknown ) ) {
// Explicitly cast to one of base class subobjects.
// Doesn't matter which one is chosen - it just has to be
// the same base class subobject each time IUnknown is requested.
IUnknown* selected = static_cast<IX*>( this );
*ppv = selected;
AddRef();
return S_OK;
} else {
continue for other interfaces
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.