问题
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 the 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?
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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...
来源:https://stackoverflow.com/questions/1742329/when-implementing-several-com-interfaces-at-once-how-do-i-upcast-to-iunknown