I've got an ATL class:
class Foo :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<Foo, &CLSID_Foo>,
public IPlugin,
public IEventSubscriber
{
// ...
};
I need to pass it to another object, like this:
pOther->MethodTakingIUnknown(this);
When I do this, I get the following error message:
error C2594: 'argument' : ambiguous conversions from 'Foo *const' to 'IUnknown *'
What am I doing wrong?
Both IPlugin
and IEventSubscriber
are derived from IUnknown
and so C++ can't decide on its own which one of IUnknown
s to cast to implicitly. You need to explicitly tell C++ which one you want. There're two options: either call GetUnknown()
(is available in every class having a COM map declared):
pOther->MethodTakingIUnknown(GetUnknown());
or explicitly cast this
to one of the base interfaces:
pOther->MethodTakingIUnknown( static_cast<IPlugin*>( this ) );
In this very case it doesn't matter which base interface you cast to - just cast to any. It only matters when you implement IUnknown::QueryInterface()
to consistently cast to the very same base every time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With