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?