ATL CAutoPtr 和 STL std::auto_ptr 之间的区别?
我正在 MFC 中编写一些代码,我想使用自动指针。 我遇到过两个不同的类,它们看起来做同样的事情:CAutoPtr
和 std::auto_ptr
人们对这两种不同的实现有什么想法?
此外,我知道有 std::tr1::shared_ptr
。 ATL/MFC 中是否有类似的 shared_ptr
?
I'm writing some code in MFC and I want to use auto pointers. I've come across two different classes that look like they do the same thing: CAutoPtr
and std::auto_ptr
What are people's thoughts about the two different implementations?
Further, I know there is std::tr1::shared_ptr
. Is there a similar shared_ptr
that is in ATL/MFC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CAutoPtr
和auto_ptr
都为您提供智能指针语义,包括所有权语义转移。CAutoPtr
是一个 ATL 类——使用 COM 构建。 它是特定操作系统的非标准扩展。 另一方面,auto_ptr
是标准 C++。 如果您想使用此类对象的容器,则必须使用CAutoPtrArray
或CAutoPtrList
。需要注意的重要一点是,有一个名为
auto_ptr_ref
的东西允许您返回auto_ptr
作为返回值。CAutoPtr
不存在这样的事情。auto_ptr
在 C++0x 中已弃用。 如果必须,请使用unique_ptr
:您可以在移动感知容器中使用它们,并且还可以从左值的不安全隐式移动中获得一些安全性。Both
CAutoPtr
andauto_ptr
give you smart pointer semantics including transfer of ownership semantics.CAutoPtr
is an ATL class -- built using COM. It is a non-standard extension for a particular OS.auto_ptr
on the other hand is standard C++. If you want to use a container of such objects you have to useCAutoPtrArray
orCAutoPtrList
.An important point to note is that there is something called
auto_ptr_ref
that allows you to returnauto_ptr
s as a return value. There is no such thing withCAutoPtr
.auto_ptr
is deprecated in C++0x. Useunique_ptr
if you have to: you can use them in move-aware containers and also get some safety from unsafe implicit moves of l-values.ATL/MFC 中与shared_ptr 最接近的是CComPtr。 它旨在用于 COM 对象,但也可以用于包含 AddRef/Release 引用计数方法的任何类。
The closest thing to shared_ptr in ATL/MFC is CComPtr. It is meant to be used on COM objects, but it can be used on any class that includes AddRef/Release reference counting methods.
CAutoPtr 是 ATL 特定的。
std:auto_ptr 和 CAutoPtr 都不提供引用计数。 看起来两者都有相同的功能。链接文本
我想有ATL/MFC 中没有shared_ptr。 shared_ptr 在 boost 库中实现。
CAutoPtr is ATL specific .
std:auto_ptr and CAutoPtr both don't provide reference counting. It looks like both have same functionality.link text
I guess there is no shared_ptr in ATL/MFC. shared_ptr is implemented in boost library.