• Articles
  • Copy constructors, assignment operators,
Published by
Jan 27, 2010 (last update: Aug 20, 2010)

Copy constructors, assignment operators, and exception safe assignment

Score: 4.2/5 (1654 votes)
*****

What is a copy constructor?

A copy constructor is a special constructor for a class/struct that is
used to make a copy of an existing instance. According to the C++
standard, the copy constructor for MyClass must have one of the
following signatures:

1
2
3
4
  MyClass( const MyClass& other );
  MyClass( MyClass& other );
  MyClass( volatile const MyClass& other );
  MyClass( volatile MyClass& other );


Note that none of the following constructors, despite the fact that
they could do the same thing as a copy constructor, are copy
constructors:

1
2
  MyClass( MyClass* other );
  MyClass( const MyClass* other );


or my personal favorite way to create an infinite loop in C++:

 
  MyClass( MyClass other );


When do I need to write a copy constructor?

First, you should understand that if you do not declare a copy
constructor, the compiler gives you one implicitly. The implicit
copy constructor does a member-wise copy of the source object.
For example, given the class:

1
2
3
4
5
  class MyClass {
      int x;
      char c;
      std::string s;
  };


the compiler-provided copy constructor is exactly equivalent to:

1
2
3
  MyClass::MyClass( const MyClass& other ) :
     x( other.x ), c( other.c ), s( other.s )
  {}


In many cases, this is sufficient. However, there are certain
circumstances where the member-wise copy version is not good enough.
By far, the most common reason the default copy constructor is not
sufficient is because the object contains raw pointers and you need
to take a "deep" copy of the pointer. That is, you don't want to
copy the pointer itself; rather you want to copy what the pointer
points to. Why do you need to take "deep" copies? This is
typically because the instance owns the pointer; that is, the
instance is responsible for calling delete on the pointer at some
point (probably the destructor). If two objects end up calling
delete on the same non-NULL pointer, heap corruption results.

Rarely you will come across a class that does not contain raw
pointers yet the default copy constructor is not sufficient.
An example of this is when you have a reference-counted object.
boost::shared_ptr<> is example.

Const correctness

When passing parameters by reference to functions or constructors, be very
careful about const correctness. Pass by non-const reference ONLY if
the function will modify the parameter and it is the intent to change
the caller's copy of the data, otherwise pass by const reference.

Why is this so important? There is a small clause in the C++ standard
that says that non-const references cannot bind to temporary objects.
A temporary object is an instance of an object that does not have a
variable name. For example:

 
  std::string( "Hello world" );


is a temporary, because we have not given it a variable name. This
is not a temporary:

 
  std::string s( "Hello world" );


because the object's name is s.

What is the practical implication of all this? Consider the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  // Improperly declared function:  parameter should be const reference:
  void print_me_bad( std::string& s ) {
      std::cout << s << std::endl;
  }

  // Properly declared function: function has no intent to modify s:
  void print_me_good( const std::string& s ) {
      std::cout << s << std::endl;
  }

  std::string hello( "Hello" );

  print_me_bad( hello );  // Compiles ok; hello is not a temporary
  print_me_bad( std::string( "World" ) );  // Compile error; temporary object
  print_me_bad( "!" ); // Compile error; compiler wants to construct temporary
                       // std::string from const char*

  print_me_good( hello ); // Compiles ok
  print_me_good( std::string( "World" ) ); // Compiles ok
  print_me_good( "!" ); // Compiles ok 


Many of the STL containers and algorithms require that an object
be copyable. Typically, this means that you need to have the
copy constructor that takes a const reference, for the above
reasons.

What is an assignment operator?

The assignment operator for a class is what allows you to use
= to assign one instance to another. For example:

1
2
  MyClass c1, c2;
  c1 = c2;  // assigns c2 to c1 


There are actually several different signatures that an
assignment operator can have:

(1) MyClass& operator=( const MyClass& rhs );
(2) MyClass& operator=( MyClass& rhs );
(3) MyClass& operator=( MyClass rhs );
(4) const MyClass& operator=( const MyClass& rhs );
(5) const MyClass& operator=( MyClass& rhs );
(6) const MyClass& operator=( MyClass rhs );
(7) MyClass operator=( const MyClass& rhs );
(8) MyClass operator=( MyClass& rhs );
(9) MyClass operator=( MyClass rhs );

These signatures permute both the return type and the parameter
type. While the return type may not be too important, choice
of the parameter type is critical.

(2), (5), and (8) pass the right-hand side by non-const reference,
and is not recommended. The problem with these signatures is that
the following code would not compile:

1
2
  MyClass c1;
  c1 = MyClass( 5, 'a', "Hello World" );  // assuming this constructor exists 


This is because the right-hand side of this assignment expression is
a temporary (un-named) object, and the C++ standard forbids the compiler
to pass a temporary object through a non-const reference parameter.

This leaves us with passing the right-hand side either by value or
by const reference. Although it would seem that passing by const
reference is more efficient than passing by value, we will see later
that for reasons of exception safety, making a temporary copy of the
source object is unavoidable, and therefore passing by value allows
us to write fewer lines of code.

When do I need to write an assignment operator?

First, you should understand that if you do not declare an
assignment operator, the compiler gives you one implicitly. The
implicit assignment operator does member-wise assignment of
each data member from the source object. For example, using
the class above, the compiler-provided assignment operator is
exactly equivalent to:

1
2
3
4
5
6
  MyClass& MyClass::operator=( const MyClass& other ) {
      x = other.x;
      c = other.c;
      s = other.s;
      return *this;
  }


In general, any time you need to write your own custom copy
constructor, you also need to write a custom assignment operator.

What is meant by Exception Safe code?

A little interlude to talk about exception safety, because programmers
often misunderstand exception handling to be exception safety.

A function which modifies some "global" state (for example, a reference
parameter, or a member function that modifies the data members of its
instance) is said to be exception safe if it leaves the global state
well-defined in the event of an exception that is thrown at any point
during the function.

What does this really mean? Well, let's take a rather contrived
(and trite) example. This class wraps an array of some user-specified
type. It has two data members: a pointer to the array and a number of
elements in the array.

1
2
3
4
5
6
7
8
9
  template< typename T >
  class MyArray {
      size_t  numElements;
      T*      pElements;

    public:
      size_t count() const { return numElements; }
      MyArray& operator=( const MyArray& rhs );
  };


Now, assignment of one MyArray to another is easy, right?

1
2
3
4
5
6
7
8
9
10
11
  template<>
  MyArray<T>::operator=( const MyArray& rhs ) {
      if( this != &rhs ) {
          delete [] pElements;
          pElements = new T[ rhs.numElements ];
          for( size_t i = 0; i < rhs.numElements; ++i )
              pElements[ i ] = rhs.pElements[ i ];
          numElements = rhs.numElements;
      }
      return *this;
  }


Well, not so fast. The problem is, the line

 
  pElements[ i ] = rhs.pElements[ i ];


could throw an exception. This line invokes operator= for type T, which
could be some user-defined type whose assignment operator might throw an
exception, perhaps an out-of-memory (std::bad_alloc) exception or some
other exception that the programmer of the user-defined type created.

What would happen if it did throw, say on copying the 3rd element of 10
total? Well, the stack is unwound until an appropriate handler is found.
Meanwhile, what is the state of our object? Well, we've reallocated our
array to hold 10 T's, but we've copied only 2 of them successfully. The
third one failed midway, and the remaining seven were never even attempted
to be copied. Furthermore, we haven't even changed numElements, so whatever
it held before, it still holds. Clearly this instance will lie about the
number of elements it contains if we call count() at this point.

But clearly it was never the intent of MyArray's programmer to have count()
give a wrong answer. Worse yet, there could be other member functions that
rely more heavily (even to the point of crashing) on numElements being correct.
Yikes -- this instance is clearly a timebomb waiting to go off.

This implementation of operator= is not exception safe: if an exception is
thrown during execution of the function, there is no telling what the state
of the object is; we can only assume that it is in such a bad state (ie,
it violates some of its own invariants) as to be unusable. If the object is
in a bad state, it might not even be possible to destroy the object without
crashing the program or causing MyArray to perhaps throw another exception.
And we know that the compiler runs destructors while unwinding the stack to
search for a handler. If an exception is thrown while unwinding the stack,
the program necessarily and unstoppably terminates.


How do I write an exception safe assignment operator?

The recommended way to write an exception safe assignment operator is via
the copy-swap idiom. What is the copy-swap idiom? Simply put, it is a two-
step algorithm: first make a copy, then swap with the copy. Here is our
exception safe version of operator=:

1
2
3
4
5
6
7
8
9
10
11
  template<>
  MyArray<T>::operator=( const MyArray& rhs ) {
      // First, make a copy of the right-hand side
      MyArray tmp( rhs );

      // Now, swap the data members with the temporary:
      std::swap( numElements, tmp.numElements );
      std::swap( pElements, tmp.pElements );

      return *this;
  }


Here's where the difference between exception handling and exception safety
is important: we haven't prevented an exception from occurring; indeed,
the copy construction of tmp from rhs may throw since it will copy T's.
But, if the copy construction does throw, notice how the state of *this
has not changed, meaning that in the face of an exception, we can guarantee
that *this is still coherent, and furthermore, we can even say that it is
left unchanged.

But, you say, what about std::swap? Could it not throw? Yes and no. The
default std::swap<>, defined in <algorithm> can throw, since std::swap<>
looks like this:

1
2
3
4
5
6
7
  template< typename T >
  swap( T& one, T& two )
  {
      T tmp( one );
      one = two;
      two = tmp;
  }


The first line runs the copy constructor of T, which can throw; the
remaining lines are assignment operators which can also throw.

HOWEVER, if you have a type T for which the default std::swap() may result
in either T's copy constructor or assignment operator throwing, you are
politely required to provide a swap() overload for your type that does not
throw. [Since swap() cannot return failure, and you are not allowed to throw,
your swap() overload must always succeed.] By requiring that swap does not
throw, the above operator= is thus exception safe: either the object is
completely copied successfully, or the left-hand side is left unchanged.

Now you'll notice that our implementation of operator= makes a temporary
copy as its first line of code. Since we have to make a copy, we might as
well let the compiler do that for us automatically, so we can change the
signature of the function to take the right-hand side by value (ie, a copy)
rather than by reference, and this allows us to eliminate one line of code:

1
2
3
4
5
6
  template<>
  MyArray<T>::operator=( MyArray tmp ) {
      std::swap( numElements, tmp.numElements );
      std::swap( pElements, tmp.pElements );
      return *this;
  }