AtlComPtrAssign 需要什么?
目前 CComPtr::operator=
是使用辅助函数 AtlComPtrAssign()
实现的,该函数具有以下签名:
IUnknown* AtlComPtrAssign( IUnknown** pp, IUnknown* lp);
并且必须像这样调用:
T* operator=( const CComPtr<T>& lp) throw()
{
if(*this!=lp) {
return static_cast<T*>(AtlComPtrAssign((IUnknown**)&p, lp));
}
return *this;
}
如果我尝试这样做,这会导致问题存储使用 CComPtr
,其中 T
是实现多个 COM 接口的类 - 到 IUnknown*
的转换变得不明确并且编译失败。
我的问题是 - 使用具有这些参数的辅助函数有什么必要?为什么不这样做:
template<classT>
T* AtlComPtrAssign( T** pp, T* lp);
并为任何合理的 T
调用它?向上转换为 IUnknown*
然后反向向下转换为 T*
有何必要?
Currently CComPtr::operator=
is implemented using a helper function AtlComPtrAssign()
that has the following signature:
IUnknown* AtlComPtrAssign( IUnknown** pp, IUnknown* lp);
and has to be called like this:
T* operator=( const CComPtr<T>& lp) throw()
{
if(*this!=lp) {
return static_cast<T*>(AtlComPtrAssign((IUnknown**)&p, lp));
}
return *this;
}
which causes problems if I try to store a use CComPtr
with T
being a class implementing more than one COM interface - the conversion to IUnknown*
becomes ambiguous and compilation fails.
My question is - what's the need for using such helper function that has exactly these parameters? Why not do like this:
template<classT>
T* AtlComPtrAssign( T** pp, T* lp);
and call it for any reasonable T
? What's the need for this upcast to IUnknown*
and then reverse downcast to T*
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信,对于早期版本的 ATL,您也可以将 CComPtr 与类一起使用。我记得使用过它,它非常方便 - 您可以同时拥有自动引用计数和原始类指针(而不是接口指针),但在某些时候,由于编译器错误,这种使用不再可用,并且需要一个自定义模板类而不是标准 CComPtr 来实现相同的功能。
IUnknown
s there, there is always just one.AtlComPtrAssign
is that in early days the function optionally resided in ATL.DLL unless choosing certain options one would prefer static link to ATL.I believe that with earlier versions of ATL you could use
CComPtr
with a class too. I remember using it and it was very convenient - you could have both automatic reference counting and raw class pointer (as opposed to interface pointer), but at some point such use was no longer available due to compiler errors and one would need a custom template class instead of standard CComPtr to achieve the same functionality.