C++ 动态数组...复制构造函数、析构函数、重载赋值运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/580622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:03:41  来源:igfitidea点击:

Dynamic array... copy constructor, destructor, overloaded assignment operator

c++constructor

提问by user69699

I am studying for my midterm exam. There is going to be a question about setting up an array dynamically, and maybe doing a copy constructor, a destructor and overloading the assignment operator. Can you please verify if I am correct. Also I don't understand what overloading the assignment operator means. Can you help me out with this?

我正在为我的期中考试而学习。将会有一个关于动态设置数组的问题,并且可能会执行复制构造函数、析构函数和重载赋值运算符。你能不能验证我是否正确。我也不明白重载赋值运算符是什么意思。你能帮我解决这个问题吗?

class A
{
int* myArray;   //basically I created a pointer called myArray, 
A()             //are my copy constructors correct? A(), and A(int size)?
{
    myArray = 0;
}
A(int size)
{
    myArray = new int[size];
}
~A()             // I think my destructor is correct
{
    delete [] myArray;
}

Can you check my code please? Also how do I overload assignment operator?

你能检查我的代码吗?另外我如何重载赋值运算符?

Thanks in advance.

提前致谢。

回答by LeopardSkinPillBoxHat

You have correctly defined 2 overloaded constructors and a destructor.

您已经正确定义了 2 个重载构造函数和一个析构函数。

However, you haven't defined an explicit copy constructorproperly.

但是,您没有正确定义显式复制构造函数

Normally the compiler will generate one for you, and this is called an implicit copy constructor.

通常编译器会为您生成一个,这称为隐式复制构造函数

The problem with the auto-generated implicit copy constructor in your particular case is that it will only perform a shallow copy of myArray, where it shares the same pointer value but hasn't allocated its own section of memory for myArray.

在您的特定情况下,自动生成的隐式复制构造函数的问题在于它只会执行myArray的浅拷贝,它共享相同的指针值,但没有为 myArray 分配自己的内存部分。

This means if you delete myArray in the original object, it will affect the copy which is most likely not what you want.

这意味着如果您删除原始对象中的 myArray,它会影响很可能不是您想要的副本。

Defining an explicit copy constructor like this will help:

像这样定义一个显式的复制构造函数会有所帮助:

A(const A& copy)
  : _size(copy.size), myArray(new int[copy.size]) 
{
    // #include <algorithm> for std::copy
    std::copy(copy.data, copy.data + copy.size, data);
}

(Source: Copied from Wikipedia)

(来源:复制自维基百科

If you define the copy constructor like this, you should not need to overload the assignment operator. If you create a new object, and assign it to the first object you created, you will successfully create an independent copy of the first object.

如果像这样定义复制构造函数,则不需要重载赋值运算符。如果您创建一个新对象,并将其分配给您创建的第一个对象,您将成功创建第一个对象的独立副本。

Edit:From this article:

编辑:这篇文章

The copy assignment operator differs from the copy constructor in that it must clean up the data members of the assignment's target (and correctly handle self-assignment) whereas the copy constructor assigns values to uninitialized data members.

复制赋值运算符与复制构造函数的不同之处在于,它必须清除赋值目标的数据成员(并正确处理自赋值),而复制构造函数将值分配给未初始化的数据成员。

回答by okutane

The copy constructor is used for creation of object based on another's instance of the same type. You don't have such. You can define it using code like this:

复制构造函数用于基于另一个相同类型的实例创建对象。你没有这样的。您可以使用如下代码定义它:

A(const A &other)
{
   myArray = new int[other._size];
   _size = other._size;
   memcpy(myArray, other.myArray, sizeof(int) * _size);
}

You should change your class, so it will store _size of array, you also need to change visibility of your constructors and destructor to public.

您应该更改您的类,以便它将存储数组的 _size,您还需要将构造函数和析构函数的可见性更改为 public。

The overloaded assignment operator should look like this:

重载赋值运算符应如下所示:

const A &operator=(const A &other)
{
   if(this == &other) return *this; // handling of self assignment, thanks for your advice, arul.
   delete[] myArray; // freeing previously used memory
   myArray = new int[other._size];
   _size = other._size;
   memcpy(myArray, other.myArray, sizeof(int) * _size);
   return *this;
}

You also can add a check of equality of array sizes in this assignment operator, so you will reuse your dynamic array without unnecessary reallocations of memory.

您还可以在此赋值运算符中添加对数组大小相等性的检查,这样您就可以重用动态数组,而无需进行不必要的内存重新分配。

回答by thrantir

when dealing with object copy and dynamic memory allocation it's a good idea to use a swap helper function

在处理对象复制和动态内存分配时,最好使用交换辅助函数

A(const A& other)
    : myArray(0)
    , _size(0)
{
   if(this != &other) {
      A my_tmp_a(other._size);
      std::copy(&other[0], &other[other._size], &my_tmp_a[0]);
      swap(my_tmp_a);
   }
}

const A& operator=(const A& other)
{
   if(this == &other) return *this;
   A my_tmp_a(other._size);
   std::copy(&other[0], &other[other._size], &my_tmp_a[0]); 
   swap(my_tmp_a);       
   return *this;
}

void swap(const A& other) {
   int* my_tmp_array = this.myArray;
   this.myArray = other.myArray;
   other.myArray = my_tmp_array;
   int my_tmp_size = this._size;
   this._size = other._size;
   other._size = my_tmp_size;
}

回答by user69699

Please make sure define three functions when you want to define one of them. Its called all or none rule They are: 1) copy constructor. 2) assignment operator. 3) destructor.

当您要定义其中一个函数时,请务必定义三个函数。它称为 all or none 规则 它们是: 1) 复制构造函数。2) 赋值运算符。3) 析构函数。

回答by David Z

Partial answer: overloading a function involves creating different versions of that function which accept different numbers or kinds of arguments. So overloading the assignment operator would involve creating several functions for the assignment operator which allow you to assign objects of various types to a variable of type A.

部分答案:重载函数涉及创建该函数的不同版本,这些版本接受不同数量或类型的参数。因此重载赋值运算符将涉及为赋值运算符创建多个函数,这些函数允许您将各种类型的对象分配给 type 的变量A