English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I would like a simple example if possible.

2006-06-14 12:11:40 · 4 answers · asked by M S 1 in Computers & Internet Programming & Design

4 answers

As others have said, the copy constructor's purpose is to ensure that a deep copy of an object is made when copied from another one.

If you don't write your own copy constructor, the compiler will provide one for you; however, it will be a shallow copy. Basically the memory in the source object will be duplicated in the destination object. If the class has any pointers, then the two objects will be pointing to the same memory space.

Once one of these objects goes out of scope, its destructor will clean up the internal pointer, and the remaining object will contain a dangling reference.

Therefore if you have a class with heap memory, you need to provide a copy constructor or make the copy constructor private so no one else uses it.


I'm pretty sure the compiler will use the copy constructor even if you don't call it directly. Consider the following:

X Class::getX()
{
X instanceX;

return instanceX;
}

When the above returns instanceX, the compiler uses the copy constructor to "move" the local instanceX's values up to the calling client.

2006-06-16 14:33:11 · answer #1 · answered by MarleyTheCat 3 · 1 0

Copy constructor's purpose is to allow you to duplicate all of the pointers and objects of a class from one an instance to another instance.
There are 2 types of copy constructors, shallow copy and deep copy.
1. Shallow Copy is simply copy all the variables and objects.
2. Deep Copy includes shallow copy and creates new pointers and reference them in new memory space.

For more detail explanation, http://en.wikipedia.org/wiki/Object_copy

2006-06-14 13:17:10 · answer #2 · answered by whatda101 2 · 0 0

Here's a practical answer. Suppose you have a class that contains pointers to dynamic memory (allocated with new).

If you use memcpy() to make a copy of the structure/class, really all you did was make copies of the pointers. So, in both copies, the pointers point to the same data.

The copy constructor would actually create a separate copy of all the dynamically allocated data so that both copies have pointers to data that are not the same.

Clear as mud?

2006-06-14 12:17:22 · answer #3 · answered by sideshot72 3 · 0 0

First, the popular constructor is used once you create an merchandise your self. for example: classification MyClass { MyClass (int v1, int v2); ... }; MyClass mc(one million,2) in this occasion you explicitly create the variable mc. The replica constructor is used once you have the choose to maintain a duplicate of yet another merchandise. it is often used whilst passing parameters to purposes. once you declare a function at the same time with: int func(MyClass m); The function recieves a duplicate of the parameter. This replica is built utilising the replica constructor. you additionally can use the replica constructor your self in case you return to a type to create a merchandise copied from yet another. classification MyClass { MyClass(int v1, v2); // popular constructor MyClass(MyClass const &different); // replica constructor ... }; MyClass::MyClass(MyClass const &different) { //replica values from different v1 = different.v1; // and so on. } void f1(MyClass m); // This func takes a duplicate of MyClass MyClass mc(one million,2); // Create MyClass merchandise with constructor MyClass mc2(mc1) // mc2 is a duplicate of mc f1(mc); // whilst f1 is termed, it gets a duplicate of mc. f1 can do despite it needs with it particularly is replica of mc. With it particularly is very own replica, despite it does would not influence the unique.

2016-12-08 20:49:38 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers