How to Use Copy Constructors in C++

By eHow Computers Editor

Rate: (2 Ratings)

A copy constructor is a special member function inside a class. It accepts a reference to an existing object of the same class type and makes a deep copy of that object. A deep copy not only copies all member fields but also copies any dynamically allocated memory. C++ compilers do copy construction automatically. You need to define a copy constructor when your class involves dynamic memory.

Instructions

Difficulty: Moderate

Things You’ll Need:

  • Intermediate C++ knowledge
  • C++ compiler, like Visual Studio 2008

Step1
Review when to call a copy constructor. There are three situations: when creating an object from an existing object of the same type, when you pass an object by value to a function and when a function returns an object.
Step2
Know when your C++ class needs a copy constructor. It boils down to this. If your class has pointers and it performs dynamic memory allocation, then you must give it an explicit copy constructor.
Step3
Look at an example where you don't have to define a constructor for a C++ class. Class X has only stack variables and no pointers. A shallow copy is sufficient to copy the values of an existing object to a new object. The compiler can do this automatically, so you don't have to define a copy constructor for class X:

class X{

int num;

public:

X(): num(0){}

~X();

};
Step4
Study an example that requires an explicit copy-constructor definition. As you can see, the size of the string, N, isn't a hard-coded value but is determined by the user. To create B from A as in the expression "Z B(A)," you have to tell the compiler how much memory to allocate for B. The place to do this is in the copy constructor, as follows:

class Z{

int N;

char* str;

public:

Z(int n): N(n){

str = new char [N];

}

Z(Z& other){

str = new char [other.GetLen()];

// copy data from the str of "other" to the str of "this"

}

~Z(){

delete [] str;

}

int GetLen() const {

return N;

}

};

Tips & Warnings

  • Imagine you omit the copy constructor in Z. When you do Z B(A), the compiler will do a shallow copy. This means, the str of B will now have the address value of the str of A, which points to string "Hello\0" somewhere in the heap. If A gets deleted, the str of B will point to nowhere. If you try to print the str of B, you get an access violation. So make sure you make explicit copy constructors where you have to.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article: How to Use Copy Constructors in C++

eHow Computers Editor

eHow Computers Editor

Category: Computers

Articles: See my other articles

Related Ads