The typical way of implementing IUnknown::QueryInterface()
is the following: use a chain of if-else-if
for each supported interface id and do the following:
if( iid == __uuidof( IInterfaceN ) ) {
*ppv = static_cast<IInterfaceN>( this );
//call Addref(), return S_OK
}
Now static_cast
is necessary here for proper pointer adjustment in multiple inheritance scenario.
Once in a while I see implementations that instead use dynamic_cast
. IMO that's a waste of time - result will be the same, it'll just take longer and make the implementation overengineered.
Is there any case when using dynamic_cast
is indeed necessary for casting this
pointer before copying it into void**
parameter of IUnknown::QueryInterface()
implementation?