开发者

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

开发者 https://www.devze.com 2022-12-14 01:18 出处:网络
I\'ve got an ATL class: class Foo : public CComObjectRootEx<CComMultiThreadModel>, public CComCoClass<Foo, &开发者_如何学JAVACLSID_Foo>,

I've got an ATL class:

class Foo :
    public CComObjectRootEx<CComMultiThreadModel>,
    public CComCoClass<Foo, &开发者_如何学JAVACLSID_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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消