'argument': ambiguous conversions from 'Foo * const' to 'IUnknown *'

I have 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, for example:

 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?

+7
com atl
source share
1 answer

Both IPlugin and IEventSubscriber are derived from IUnknown , so C ++ cannot independently decide which of IUnknown use implicitly. You need to explicitly specify the C ++ you want. There are two options: either call GetUnknown() (available in each class with a declared COM map):

 pOther->MethodTakingIUnknown(GetUnknown()); 

or explicitly attach this to one of the base interfaces:

 pOther->MethodTakingIUnknown( static_cast<IPlugin*>( this ) ); 

In this case, it does not matter which base interface you belong to - just add to any. This only matters when you implement IUnknown::QueryInterface() to sequentially IUnknown::QueryInterface() to the same base every time.

+9
source share

All Articles