[英]Using the copy-and-swap idiom, how does the destructor of the copied object not deallocate pointed to memory?
I was reading the following question: 我在阅读以下问题:
What is the copy-and-swap idiom? 什么是复制和交换习语?
I was under the impression that when an object is passed by value, it's pointers and values are copied, but the memory pointed to by the passed object's pointers is not copied. 我给人的印象是,当对象通过值传递时,它是指针和值被复制,但是传递的对象指针所指向的内存没有被复制。 So when overloading the assignment operator like such from the linked to example:
因此,当重载赋值运算符时,如链接示例中所示:
#include <algorithm> // std::copy
#include <cstddef> // std::size_t
class dumb_array
{
public:
// (default) constructor
dumb_array(std::size_t size = 0)
: mSize(size),
mArray(mSize ? new int[mSize]() : 0)
{
}
// copy-constructor
dumb_array(const dumb_array& other)
: mSize(other.mSize),
mArray(mSize ? new int[mSize] : 0),
{
// note that this is non-throwing, because of the data
// types being used; more attention to detail with regards
// to exceptions must be given in a more general case, however
std::copy(other.mArray, other.mArray + mSize, mArray);
}
// destructor
~dumb_array()
{
delete [] mArray;
}
friend void swap(dumb_array& first, dumb_array& second) // nothrow
{
// enable ADL (not necessary in our case, but good practice)
using std::swap;
// by swapping the members of two classes,
// the two classes are effectively swapped
swap(first.mSize, second.mSize);
swap(first.mArray, second.mArray);
}
dumb_array& operator=(dumb_array other) // (1)
{
swap(*this, other); // (2)
return *this;
}
private:
std::size_t mSize;
int* mArray;
};
... how does the destructor of the copied object not eliminate the pointed to resource, mArray
? ...复制对象的析构函数如何不消除指向资源的
mArray
? Doesn't the object on which the assignment is being done now have a copied mArray
pointer to potentially deallocated memory? 现在,要在其上进行分配的对象是否具有指向潜在释放内存的复制
mArray
指针? Does the line swap(first.mArray, second.mArray);
线是否
swap(first.mArray, second.mArray);
allocate new memory and copy the contents of the previous array? 分配新的内存并复制先前数组的内容?
As your copy constructor implemented, 当您的副本构造函数实现后,
dumb_array(const dumb_array& other)
: mSize(other.mSize),
mArray(mSize ? new int[mSize] : 0),
the mArray
inside dumb_array
was deeply copied. mArray
内部的dumb_array
被深深复制。 Not just copied the pointer, but created a brand new array and filled with copies of content by std::copy()
. 不仅复制了指针,还创建了一个全新的数组,并通过
std::copy()
填充了内容的std::copy()
。
Thus, when operator=(dumb_array other)
is executed, this->mArray
and other.mArray
(which is a copy since the parameter is an object instead of a reference) are two different arrays. 因此,当执行
operator=(dumb_array other)
, this->mArray
和other.mArray
(由于参数是对象而不是引用,因此是一个副本)是两个不同的数组。 After the swap()
, other.mArray
holds the pointer originally held by this->mArray
. 在
swap()
, other.mArray
保留最初由this->mArray
保留的指针。 And when operator=()
returns, other.mArray
could just be deleted by ~dumb_array()
. 当
operator=()
返回时, other.mArray
可以被~dumb_array()
删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.