Dynamic array… copy constructor, destructor, overl

2020-05-19 09:47发布

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.

5条回答
我命由我不由天
2楼-- · 2020-05-19 10:20

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.

查看更多
来,给爷笑一个
3楼-- · 2020-05-19 10:30

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.

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.

查看更多
Juvenile、少年°
4楼-- · 2020-05-19 10:36

You have correctly defined 2 overloaded constructors and a destructor.

However, you haven't defined an explicit copy constructor properly.

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.

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

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.

查看更多
祖国的老花朵
5楼-- · 2020-05-19 10:36

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;
}
查看更多
家丑人穷心不美
6楼-- · 2020-05-19 10:37

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.

查看更多
登录 后发表回答