Qt 和 auto_ptr

发布于 2024-09-02 03:17:39 字数 587 浏览 13 评论 0原文

我刚刚发现了 auto_ptr 的概念并且很喜欢它!由于 Qt 通常需要 QList 或 QVector<(某些 QObject 或 QWidget)*>,是否有任何具体原因应该避免 auto_ptr 。如果我是对的,它允许你

std::vector<MyClass*> vec;
/* add several elements to the vector and do stuff with them */
for(size_t i=0; i<vec.length(); ++i)
{
    delete vec[i];
}
vec.clear();

用更短的东西来替换它(即不需要清理)

std::vector<auto_ptr<MyClass>> vec;
/* add several elements to the vector and do stuff with them */
// no need for the delete loop

...Qt 仍然可以使用 auto_ptr 来发挥其共享内存魔力吗?父子自动内存管理是否仍然透明地发挥作用?谢谢

I just discovered the concept of an auto_ptr and am liking it! As Qt often requires a QList or QVector<(some QObject or QWidget) *>, is there any concrete reason why auto_ptr should be avoided. If I'm right, it allows you to replace this:

std::vector<MyClass*> vec;
/* add several elements to the vector and do stuff with them */
for(size_t i=0; i<vec.length(); ++i)
{
    delete vec[i];
}
vec.clear();

with somthing much shorter (ie no cleanup)

std::vector<auto_ptr<MyClass>> vec;
/* add several elements to the vector and do stuff with them */
// no need for the delete loop

...Can Qt still work its shared memory magic with auto_ptr's? Does the parent-child automagic memory management still function transparently? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

陌路终见情 2024-09-09 03:17:39

Qt 有自己的智能指针类,它们在很多方面都优于 std::auto_ptr,特别是其中一些可以毫无问题地放入容器中。 std::auto_ptr 具有所有权转移复制语义,因此如果您尝试将其放入容器中,将无法按预期工作。

尝试使用 QSharedPointer。它是一个引用计数智能指针,当智能指针不再有副本时,它会删除其包含的对象。与 std::auto_ptr 不同,同一个 QSharedPointer 可以同时有多个副本,因此它可以很好地与容器配合使用。

Qt has its own smart pointer classes that are in many ways superior to std::auto_ptr, specifically in that some of them can be put into containers with no issues. std::auto_ptr has ownership-transfer copy semantics, and so will not work as expected if you try to put it into a container.

Try using QSharedPointer. It's a reference counted smart pointer that deletes its contained object when there are no more copies of the smart pointer left. Unlike std::auto_ptr there can be multiple copies of the same QSharedPointer at once, and therefore it plays well with containers.

我不在是我 2024-09-09 03:17:39

如果您想要一个拥有指针所有权的容器,那么请查看 boost 指针容器。

您将指针放入容器中,但与其他容器一样,它们会被视为普通对象,这使得更容易通过标准算法使用它们(即无需编写包装类)。当指针容器超出范围时,它将对容器中的所有指针调用删除:

boost::ptr_vector<MyClass>   v;
v.push_back(new MyClass(12));

std::for_each(v.begin(), v.end(), DoStuff());

// Destroyed here.

If you want a container that has ownership of pointers then look at boost pointer containers.

You put pointers into the container, but like the other containers they are then treated like normal objects which makes it easier to use them with standard algorithms (i.e. no need to write wrapper classes). When the pointer container goes out of scope it will call delete on all pointers in the container:

boost::ptr_vector<MyClass>   v;
v.push_back(new MyClass(12));

std::for_each(v.begin(), v.end(), DoStuff());

// Destroyed here.
久伴你 2024-09-09 03:17:39

std::auto_ptr 不能在 std::vector 中使用,因为 std::vector 期望能够复制其内容,并且您无法在正常意义上复制 std::auto_ptr 。复制意味着最终会得到两个相同的东西,如果你有两个相同的 std::auto_ptr ,那么当它们超出范围时,它们所指向的内容将被双重释放。 (所发生的情况是,被复制的 auto_ptr 的内部指针被清零,而被复制到的指针现在是旧的指针。)

使用 shared_ptr,通常可作为 boost::shared_ptr 或在 Visual C++ std::tr1::shared_ptr 上使用,并且将位于 C++0x 标准库中,或者使用 Qt 拥有的任何东西。它们可以被复制,因此可以放入容器中。

std::auto_ptr cannot be used in std::vector, because std::vector expects to be able to copy its contents, and you can't copy a std::auto_ptr in the normal sense. Copying means winding up with two identical things, and if you had two identical std::auto_ptrs what they pointed to would be double-freed when they went out of scope. (What happens instead is that the auto_ptr being copied from has its internal pointer zeroed out, and the one being copied to is now what the old one used to be.)

Use shared_ptr, which is often available as boost::shared_ptr or on Visual C++ std::tr1::shared_ptr, and which will be in the C++0x standard library, or use whatever Qt has. Those can be copied, and therefore can go into containers.

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