1

Being beginner in C++, I have found following code while reading on copy constructor and assignment operator.

Right below the code, I found it needs operator=.

I don't understand why there is rule of three, just by having copy constructor can't this work?

#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]() : nullptr)
    {
    }

    // copy-constructor
    dumb_array(const dumb_array& other)
        : mSize(other.mSize),
          mArray(mSize ? new int[mSize] : nullptr)
    {
        // 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;
    }

private:
    std::size_t mSize;
    int* mArray;
};

This class almost manages the array successfully, but it needs operator= to work correctly.

  • 3
    I'm not sure I understand your question. Are you asking "does my copy constructor cause a memory leak?" or are you asking "does my class need to define `operator=`"? – Cory Kramer Nov 12 '21 at 19:49
  • 2
    Copy constructor and copy assignment operator are two separate things. Your class needs both – UnholySheep Nov 12 '21 at 19:50
  • 2
    The copy constructor does not cause a memory leak. The lack of a user-defined copy assignment operator will. Right now you have a default copy assignment operator that will make both `dumb_array`s point to the same memory. They'll be stepping on each other's toes. – JohnFilleau Nov 12 '21 at 19:52
  • 1
    Please read about [rule of three/five/zero](https://en.cppreference.com/w/cpp/language/rule_of_three). – n. m. could be an AI Nov 12 '21 at 19:54
  • Quick and damn near foolproof way to write an assignment operator: [What is the copy-and-swap idiom?](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). It can be slower than optimal. Profiling will let you know if you need a more complicated solution. – user4581301 Nov 12 '21 at 19:56
  • 1
    **Rule of 3:** If a class requires a user-defined destructor, a user-defined copy constructor, or a user-defined copy assignment operator, it almost certainly requires all three. **Rule of 5 (C++11):** Because the presence of a user-defined destructor, copy-constructor, or copy-assignment operator prevents implicit definition of the move constructor and the move assignment operator, any class for which move semantics are desirable, has to declare all five special member functions: https://en.cppreference.com/w/cpp/language/rule_of_three – paulsm4 Nov 12 '21 at 20:18
  • A copy constructor is for building a new object that is a copy of an existing one. A copy assignment operator is for modifying an existing object so that it becomes a copy of another existing object. They do different things, and therefore you need both. – Miles Budnek Nov 12 '21 at 20:29

0 Answers0