为什么在复制赋值运算符的定义中需要删除? [英] Why delete is needed in the definition of the copy-assignment operator?
问题描述
我是一个C ++初学者。我在C ++ Primer(第5版)中做练习。我找到了一个引用
从Github练习13.8(这里),如下所示。
I am a C++ beginner. And I am doing the exercises in C++ Primer (5th Edition). I found a reference to Exercise 13.8 from Github (Here), which is shown below.
#include <string>
#include <iostream>
using std::cout;
using std::endl;
class HasPtr {
public:
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { }
HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { }
HasPtr& operator=(const HasPtr &hp) {
std::string *new_ps = new std::string(*hp.ps);
delete ps; // I don't know why it is needed here?
// But when I delete this line, it also works.
ps = new_ps;
i = hp.i;
return *this;
}
void print() {
cout << *(this->ps) << endl;
cout << this->i << endl;
}
private:
std::string *ps;
int i;
};
int main() {
HasPtr hp1("hello"), hp2("world");
hp1.print();
hp1 = hp2;
cout << "After the assignment:" << endl;
hp1.print();
}
令我困惑的是 HasPtr& operator =(const HasPtr& hp)
函数。我不知道为什么删除ps;
在这里需要。我认为这是一个错误,但它工作时,我编译代码。但是,当我删除 delete ps;
的行时,它也工作。所以,我不知道是否需要 delete ps;
,如果是保留的话有什么优点。
What makes me confusing is the HasPtr& operator=(const HasPtr &hp)
function. I don't know why delete ps;
is needed here. I thought it was an error, but it worked when I compiled the code. However, it also works when I delete the line of delete ps;
. So, I do not know whether delete ps;
is needed and what is the advantage if it is reserved.
推荐答案
HasPtr :: ps
是一个堆分配的 std :: string
指针。
HasPtr::ps
is an heap-allocated std::string
pointer.
新
所有 hasPtr
构造函数。因此,当 HasPtr :: ps
被替换为另一个堆分配的指针时,必须使用 delete
以避免内存泄漏。
It is allocated and constructed using new
in all HasPtr
constructors. Therefore, when HasPtr::ps
gets replaced by another heap-allocated pointer, the existing memory has to be released using delete
to avoid memory leaks.
注意在现代C ++中,您应该几乎从不使用 new
和 delete
喜欢这个。请改用智能指针,例如 std :: unique_ptr
或 std :: shared_ptr
Note that, in modern C++, you should almost never use new
and delete
for managing objects like this. Use smart pointers instead, like std::unique_ptr
or std::shared_ptr
, which take care of memory management for you, safely and conveniently.
我仍然建议您熟悉新
和 delete
,因为大量的现有代码使用它们。 cppreference.com
是查找语言的详细信息的好地方
I still suggest getting familiar with new
and delete
, as an huge amount of existing code makes use of them. cppreference.com
is a great place to find detailed information about the language.
由于 Jan Hudec 在评论中提到,在堆上存储 std :: string
通常很愚蠢 - std :: string
是一个包装器一个堆分配的字符数组,它已经为你管理了内存。
As Jan Hudec mentioned in the comments, it is generally pretty silly to store an std::string
on the heap - std::string
is a wrapper around an heap-allocated character array, which already manages the memory for you.
这篇关于为什么在复制赋值运算符的定义中需要删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!