Is using implicit conversion for upcast instead of QueryInterface () legal with multiple inheritance?

Suppose I have a class that implements two or more COM interfaces (exactly like here ):

class CMyClass : public IInterface1, public IInterface2 { 
};

QueryInterface() must return the same pointer for each request of the same interface (for the correct adjustment of the pointer, it needs an explicit upcast):

if( iid == __uuidof( IUnknown ) ) { 
    *ppv = static_cast<IInterface1*>( this );
    //call Addref(), return S_OK 
} else if( iid == __uuidof( IInterface1 ) ) {
    *ppv = static_cast<IInterface1*>( this );
    //call Addref(), return S_OK 
} else if( iid == __uuidof( IInterface2 ) ) {
    *ppv = static_cast<IInterface2*>( this );
    //call Addref(), return S_OK 
} else {
    *ppv = 0;
    return E_NOINTERFACE;
}

Now there are two in the object IUnknown- one of them is the base IInterface1, and the other is the base IInterface2. And they are in different subobjects .

, QueryInterface() IInterface2 - , QueryInterface() IUnknown. . IInterface2* , IUnknown*, ++ , , QueryInterface() IUnknown*. , QueryInterface() IUnknown , .

COM? , , ?

+5
4

COM , , . QI , QI IID_Unknown , . QI .

, COM- , IUnknown, , QI . , , QI.

+3

, . IInterface1 IInterface2 . IInterface1 IInterface2 QueryInterface(). , CMyClass IInterface1 IInterface2 ( CMyClass - IInterface1 IInterface1 ).

, CMyClass QueryInterface(), AddRef() Release() . QueryInterface() CMyClass static_cast<IUnknown*>.

: ! . - .

OK. , IInterface1 IUnknown, IInterface2 IUnknown, . ! , , . ( QueryInterface(), AddRef() Release()), , . !

, COM C. , QueryInterface(), AddRef() Release(), QueryInterface(). , COM, , COM, (, vtable ).

. QueryInterface(), AddRef() Release(). . , - , , , . . ++, vtables QueryInterface(), AddRef(), Release() .., vtables .

vtables, Microsoft __declspec(novtable) ATL_NO_VTABLE, .

+2

Hans , QueryInterface .

COM- QueryInterface. , : IInterface1 * IInterface2 * IUnknown * .

++ .

, , COM- .

MSDN:

. , QueryInterface IUnknown . QueryInterface (IID_IUnknown,...) on , , ( COM-).

, , COM- - , .

COM QueryInterface . QueryInterface(IID_IUnknown, ...) , IUnknown *, . , .

, , - . COM, , .

+2

interface IInterface1 : IDispatch interface IInterface2 : IDispatch, QI IUnknown IInterface1 IInterface2

...

QI IDispatch IInterface1 QI IDispatch IInterface2.

, () . IUnknown - .

0

All Articles