The Big Three in C++

Constructor, destructors and assignment operators

There is a rule of thumb in C++ that if a class defines a destructor, constructor and copy assignment operator – then it should explicitly define these and not rely on their default implementation.

Why do we need them?

In the example below, the absence of an explicit copy constructor will simply make an exact copy of the class and you end up with two classes pointing to the same memory address – not what you want.  When you delete the array pointer in one class for example, the other class will be pointing to memory to which you have no idea as what it contains.

Consider the following example class which is used to house an array of integers plus its size. It is written in such a way as to guarantee a crash:

#include ,algorithm>

class Array
{
private:
    int size;
    int* vals;  

public:
    ~Array();
    Array( int s, int* v );
};

Array::~Array()
{
   delete vals;
   vals = NULL;
}

Array::Array( int s, int* v )
{
    size = s;
    vals = new int[ size ];
    std::copy( v, v + size, vals );
}

int main()
{
   int vals[ 4 ] = { 1, 2, 3, 4 };

   Array a1( 4, vals );
   Array a2( a1 );
   return 0;
}
// Bam!

Once the program goes out of scope (exits the main loop), the class destructor is called. Twice. Once for Array object a1 and then for a2. Remember that the compiler-generated default copy constructor simply makes a copy of the pointer vals.  It does not know that it also needs to allocate memory for a new vals.  When a1 is deleted, its destructor frees up vals.  Subsequent use of vals in the other instance a2 when once more trying to delete it in the destructor causes the program to fall over, since that instance of vals does not exist any more.

Using explicitly defined copy constructors

There is clearly a need to explicitly define a copy constructor that correctly copies vals.  Suppose someone has now defined an acceptable copy constructor, in addition to the default constructor:

class Array
{
private:
   int size;
   int* vals;  

public:
   ~Array();
   Array( int s, int* v );
   Array( const Array& a );
};

Array::Array( const Array &a )
{
   size = a.size;
   vals = new int[ a.size ];
   std::copy( a.vals, a.vals + size, vals );
}

This improves the situation in that the code will not crash when exiting the main loop. But we’re not in the clear yet. It is still vulnerable to a crash as soon as someone has a go at assigning an instance of Array.

Now try this:

int main()
{
   int vals[ 4 ] = { 1, 2, 3, 4 };
   Array a1( 4, vals );
   Array a2( a1 );
   a1 = a2;
   return 0;
}
// Bam!

As before two instances of vals are allocated, and the two then get deleted just fine. So why the crash again?  As before, both instances of Array are pointing to the same instance of vals.  This is due to the default assignment operator that gets called, unless one is explicitly defined.  It only knows how to assign the pointer to vals.  Consequently you also need to explicitly define an assignment operator to complement the copy constructor. Full code listing as follows.

The full picture: copy constructors and assignment operator

#include <algorithm>

class Array
{
private:
   int size;
   int* vals;

public:
   ~Array();
   Array( int s, int* v );
   Array( const Array& a );
   Array& operator=( const Array& a );
};

Array::~Array()
{
   delete vals;
   vals = NULL;
}

Array::Array( int s, int* v )
{
   size = s;
   vals = new int[ size ];
   std::copy( v, v + size, vals );
}

Array::Array( const Array &a )
{
   size = a.size;
   vals = new int[ a.size ];
   std::copy( a.vals, a.vals + size, vals );
}

Array& Array::operator =(const Array &a)
{
   if ( &a != this )
   {
      size = a.size;
      vals = new int[ a.size ];
      std::copy( a.vals, a.vals + size, vals );
   }
   return *this;
}

int main()
{
   int vals[ 4 ] = { 1, 2, 3, 4 };
   Array a1( 4, vals );
   Array a2( a1 );
   a1 = a2;
   return 0;
}