Suppose my COM object implements two or more COM interfaces:
class CMyClass : public IPersistFile, public IPersistStream {
};
when implementing QueryInterface() I need to be able to return an IUnknown* pointer. Since both base interfaces are derived from IUnknown I cannot upcast implicitly - such upcast would be umbiguous. To upcast explicitly I need to use either of the two ways:
if( iid == __uuidof( IUnknown ) ) {
*ppv = static_cast<IPersistFile*>( this );
static_cast<IPersistFile*>( this )->AddRef();
return S_OK;
}
or
if( iid == __uuidof( IUnknown ) ) {
*ppv = static_cast<IPersistStream*>( this );
static_cast<IPersistStream*>( this )->AddRef();
return S_OK;
}
Looks like the only requirement is that whenever QI() is called on an object it returns 开发者_如何学Gothe same pointer each time and I meet that requirement if I choose any of the casts and just stick to it.
Which upcast should I choose and why?
Mark Ransom already gave the correct answer - any will do, as long as it's consistent - but picking the first one has one minor advantage. Due to layout rules, the IUnknown*
of the first interface will point to the start of the object. Any other IUnknown*
will point to subsequent vtable pointers elsewhere in the object. For debugging purposes, it's very useful to know where ano object begins in memory.
It doesn't matter which upcast you use, only that you use the same one always. I'd just pick a convention, such as always returning the first one declared in the inheritance list.
IUknown is unimplemented. You need to provide all the implementation of IUnknown. Thus, to QI IUknown, you return the this pointer.
AddRef, Release and QI are all implemented by you and not on the parent interface anyway so you have no issues just CALLING addref, no casting is required.
Usually in cases where you want to cast to IUnknown an object with multiple inheritence from IUnknown, you cast it to one of its interface then cast to IUnknown...
精彩评论