Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

com

atl

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?

like image 222
Roger Lipscombe Avatar asked Dec 09 '09 11:12

Roger Lipscombe


1 Answers

Both IPlugin and IEventSubscriber are derived from IUnknown and so C++ can't decide on its own which one of IUnknowns 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.

like image 51
sharptooth Avatar answered Nov 27 '22 00:11

sharptooth