如果std :: vector元素'commitits suicide'(使用delete this;)会发生什么? [英] What will happen if a std::vector element 'commits suicide' (using delete this;)?

查看:153
本文介绍了如果std :: vector元素'commitits suicide'(使用delete this;)会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个向量 Item s

vector<Item*> items; //{item1, item2, item3}

然后,在代码的其他部分,

Then, in other part of the code,

items[1]->suicide();

其中 suicide p>

where the suicide function is:

void Item::suicide()
{
   delete this;
}

矢量大小和如何安排现在?
这是好吗这样做吗?

编辑 :如果输出的所需排列 {item1,item3} ,size为 2 ,并且没有悬挂指针,如何以自毁方式(从 item2 本身)?

Edit (may I ask an additional question?): If the desired arrangement of the output is {item1, item3}, size is 2, and no dangling pointer, how to do it in a self-destructing way (from the item2 itself)?

编辑2 :感谢所有的答案!真棒。所以我终于决定,并找到了从对象外面做的方式,因为它是一个坏的做法和不必要的复杂

Edit 2 : Thanks for all the answers! Awesome. So I finally decided and found the way to do it from outside of the object because it was a bad practice and unnecessarily complicated

推荐答案

什么是项目矢量大小和如何安排现在?一样。函数调用不会改变向量内容或大小。它只释放指针指向的内存。

What is items vector size and how it's arrangement now? The same. The function call does not change the vector contents nor size at all. It just frees the memory the pointer is pointing to.

可以这样做吗?更确切地说:它是合法的C ++吗?是。是好的风格编程吗?让我详细说明后者:

Is it okay to do this? More precisely: Is it legal C++? Yes. Is it good style programming? No. Let me elaborate on the latter:


  • 应该分开关注:谁负责内存管理? Item 或类 Item 本身的容器或用户?

  • There should be a separation of concerns: Who's responsible for memory management? The container or the user of the class Item or the class Item itself?

通常容器或用户应该这样做,因为他知道发生了什么。

Typically the container or user should do that, because he knows what's going on.

这样做的方法是什么?现代和安全C ++代码中的内存管理大多使用诸如 std :: shared_ptr std :: unique_ptr std :: vector std :: map 的容器。

What's the way to do that? Memory management in modern and safe C++ code is mostly done using smart pointers like std::shared_ptr and std::unique_ptr and containers like std::vector and std::map.

如果类 Item 是值类型(这意味着您可以简单地复制它,没有在虚拟函数方面的多态行为),那么只需使用 std :: vector< Item> 代替你的代码。一旦元素从容器中删除,析构函数将被自动调用。容器为你做它。

If the class Item is a value type (that means you can simply copy it and it has no polymorphic behavior in terms of virtual functions), then just use std::vector<Item> instead for your code. Destructors will be called automatically as soon as an element is removed from the container. The container does it for you.

如果类 Item 具有多态行为并且可以用作基类, code> std :: vector< std :: unique_ptr< Item>> std :: vector< std :: shrared_ptr< Item> c $ c>,并且更喜欢 std :: unique_ptr 解决方案,因为它增加了更少的开销。一旦停止引用某个对象,它将被您正在使用的智能指针的析构函数自动删除。

If the class Item has polymorphic behavior and can be used as a base class, then use std::vector<std::unique_ptr<Item>> or std::vector<std::shrared_ptr<Item>> instead and prefer the std::unique_ptr solution, because it adds less overhead. As soon as you stop referring to an object, it will be deleted automatically by the destructor of the smart pointer you are using. You (almost) don't need to worry about memory leaks anymore.

你可以产生内存泄漏的唯一方法是你有对象包含 std :: shared_ptrs 以循环方式相互引用。使用 std :: unique_ptrs 可以防止这种麻烦。另一种出路是 std :: weak_ptrs

The only way you can produce memory leaks is that you have objects that contain std::shared_ptrs that refer to each other in cyclic way. Using std::unique_ptrs can prevent this kind of trouble. Another way out are std::weak_ptrs.

底线:不提供函数 suicide()。而是将责任完全放在调用代码的手中。使用标准库容器和智能指针管理您的内存。

Bottom line: Don't provide a function suicide(). Instead put the responsibility solely into the hands of the calling code. Use standard library containers and smart pointers to manage your memory.

修改:关于您修改的问题。只要写

Concerning the question in your edit. Just write

items.erase( items.begin() + 1 );

这将适用于所有类型: std :: vector 的值或指针。您可以找到 std :: vector 和C ++标准库的良好文档此处

This will work for all types: std::vector of values or pointers. You can find a good documentation of std::vector and the C++ Standard library here.

这篇关于如果std :: vector元素'commitits suicide'(使用delete this;)会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆