ATL CAutoPtr 和 STL std::auto_ptr 之间的区别?

发布于 2024-07-15 09:27:23 字数 223 浏览 12 评论 0原文

我正在 MFC 中编写一些代码,我想使用自动指针。 我遇到过两个不同的类,它们看起来做同样的事情:CAutoPtrstd::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾其所爱 2024-07-22 09:27:23

CAutoPtrauto_ptr 都为您提供智能指针语义,包括所有权语义转移。 CAutoPtr 是一个 ATL 类——使用 COM 构建。 它是特定操作系统的非标准扩展。 另一方面,auto_ptr 是标准 C++。 如果您想使用此类对象的容器,则必须使用 CAutoPtrArrayCAutoPtrList

需要注意的重要一点是,有一个名为 auto_ptr_ref 的东西允许您返回 auto_ptr 作为返回值。 CAutoPtr 不存在这样的事情。

auto_ptr 在 C++0x 中已弃用。 如果必须,请使用 unique_ptr:您可以在移动感知容器中使用它们,并且还可以从左值的不安全隐式移动中获得一些安全性。

Both CAutoPtr and auto_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 use CAutoPtrArray or CAutoPtrList.

An important point to note is that there is something called auto_ptr_ref that allows you to return auto_ptrs as a return value. There is no such thing with CAutoPtr.

auto_ptr is deprecated in C++0x. Use unique_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.

手心的温暖 2024-07-22 09:27:23

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.

淡紫姑娘! 2024-07-22 09:27:23

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文