C++ Copy Constructor


First, what IS a copy constructor? Here is an example:

  Complex A(13,666);
  Complex B(A);

In the above example, the programmer has allowed for the construction of B using A. B should have 13, 666 as its elements after construction.

If you have a memory allocation in your constructor, that is if you use the new keyword in your constructor, then you should have 3 other things: 1) a destructor 2) a copy constructor 3) an overload of the assignement operator.

Why you ask? I will tell you... Remember that the default behavior of C++ is to do copy and assignment by a shallow copy. In other words, you do not get new memory allocated to hold the contents of the original object. If you want memory to be allocated, you must manually implement the behavior.


For the updated discussion of assignment overloading, which now contains a copy constructor:

Assignment Overloading

For a more complete discussion of the copy constructor go to the following site:

C++ Copy Constructor FAQ