使用隐式转换为upcast而不是QueryInterface()法与多继承? [英] Is using implicit conversion for an upcast instead of QueryInterface() legal with multiple inheritance?

查看:166
本文介绍了使用隐式转换为upcast而不是QueryInterface()法与多继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类实现两个或多个COM接口(正如):

  class CMyClass:public IInterface1,public IInterface2 {
};

QueryInterface()必须返回相同的指针对于同一接口的每个请求(它需要一个明确的upcast适当的指针调整):

  if(iid == __uuidof IUnknown)){
* ppv = static_cast< IInterface1 *>(this);
//调用Addref(),return S_OK
} else if(iid == __uuidof(IInterface1)){
* ppv = static_cast< IInterface1 *>
//调用Addref(),返回S_OK
} else if(iid == __uuidof(IInterface2)){
* ppv = static_cast< IInterface2 *>
//调用Addref(),return S_OK
} else {
* ppv = 0;
return E_NOINTERFACE;
}

现在有两个 IUnknown s在对象中 - 一个是 IInterface1 的基础,另一个是 IInterface2 的基础。和它们在不同的子对象中。



让我们假设我叫 QueryInterface() IInterface2 - - 时,返回的指针将不同于 QueryInterface() 。到现在为止还挺好。然后我可以将检索到的 IInterface2 * 传递给任何接受 IUnknown * 的函数,感谢C ++隐式转换指针接受,但它不会与 QueryInterface() for IUnknown * 检索的指针相同。事实上,如果该函数在被调用时立即调用 QueryInterface(),因为 IUnknown p>

这是否合法的COM?当我有一个指针指向一个乘法继承的对象,并允许一个隐式转换时,如何处理情况?

解决方案

没有关于接口身份的规则,只有对象身份。 QI的第一条规则说,如果两个接口指针上的IID_Unknown的QI由相同的对象实现,则它们必须返回相同的指针。



没有对接口标识的保证,COM方法不能假设它获得相同的IUnknown指针传递,当它调用QI上的指针。因此,如果对象身份需要被证明,则需要单独的QI。


Assume I have a class implementing two or more COM interfaces (exactly as here):

class CMyClass : public IInterface1, public IInterface2 { 
};

QueryInterface() must return the same pointer for each request of the same interface (it needs an explicit upcast for proper pointer adjustment):

if( iid == __uuidof( IUnknown ) ) { 
    *ppv = static_cast<IInterface1*>( this );
    //call Addref(), return S_OK 
} else if( iid == __uuidof( IInterface1 ) ) {
    *ppv = static_cast<IInterface1*>( this );
    //call Addref(), return S_OK 
} else if( iid == __uuidof( IInterface2 ) ) {
    *ppv = static_cast<IInterface2*>( this );
    //call Addref(), return S_OK 
} else {
    *ppv = 0;
    return E_NOINTERFACE;
}

now there're two IUnknowns in the object - one is the base of IInterface1 and the other is the base of IInterface2. And they are in different subobjects.

Let's pretend I called QueryInterface() for IInterface2 - the pointer returned will be different from the pointer returned when I call QueryInterface() for IUnknown. So far so good. Then I can pass the retrieved IInterface2* into any function accepting IUnknown* and thanks to C++ implicit conversion the pointer will be accepted, but it will be not the same pointer that QueryInterface() for IUnknown* would retrieve. In fact if that function calls QueryInterface() for IUnknown immediately upon being called it will retrieve a different pointer.

Is this legal in terms of COM? How do I handle situations when I have a pointer to a multiply-inherited object and I allow an implicit upcast?

解决方案

COM has no rules regarding interface identity, only of object identity. The first rule of QI says that a QI on IID_Unknown on two interface pointers must return the same pointer if they are implemented by the same object. Your QI implementation does this correctly.

Without a guarantee for interface identity, a COM method cannot assume that it gets the same IUnknown pointer passed that it will retrieve when it calls QI on that pointer. So if object identity needs to be proven then a separate QI is required.

这篇关于使用隐式转换为upcast而不是QueryInterface()法与多继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆