将智能指针分配给_variant_t是否需要手动AddRef()?
这是一个示例片段:
_variant_t var;
var.vt = VT_UNKNOWN;
var.punkVal = unknownInterfaceSmartPointer;
unknownInterfaceSmartPointer->AddRef(); // Question Statement
// Setting unknownInterfaceSmartPointer to some other container
这里是否真的需要手动 AddRef?遵循意愿是比这更好的方法:
_variant_t var(unknownInterfaceSmartPointer.GetInterfacePointer());
你的想法?
Here is an example snippet:
_variant_t var;
var.vt = VT_UNKNOWN;
var.punkVal = unknownInterfaceSmartPointer;
unknownInterfaceSmartPointer->AddRef(); // Question Statement
// Setting unknownInterfaceSmartPointer to some other container
Whether manual AddRef is really needed here? Following will is better way than this:
_variant_t var(unknownInterfaceSmartPointer.GetInterfacePointer());
your thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您直接分配给
punkVal
成员(如第一个片段中所示),那么您需要一个AddRef()
- 没有人会为您做这件事。更好的方法是使用_variant_t
的operator=()
成员:或者按照您的建议操作 - 使用转换构造函数:
后两种变体都很好,但都不好需要在代码中添加
AddRef()
- 它将在_variant_t
实现中完成。此外,它们是有益的,因为如果您重新分配给相同的变体,实现将为您进行适当的清理:If you directly assign to
punkVal
member (as in your first snippet) then you need anAddRef()
- noone will do it for you. A much better way would be to useoperator=()
member of_variant_t
:or do as you suggest - use a conversion constructor:
Both latter variants are good and don't require an
AddRef()
in your code - it will be done inside_variant_t
implementation. Furthermore, they are beneficial because if you reassign to the same variant the implementation will do proper cleanup for you: