tags:

views:

77

answers:

1

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?

+1  A: 

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.

sharptooth
+1: Better answer than mine, because it covers the non-ATL case as well.
Roger Lipscombe