The copy constructor in C ++

copy constructor in C ++,  c ++ copy constructor, C ++ programming for beginnersWhen beginners learn programming, first thing, when considering a new topic, the question arises – which requires one or another “thing” What is to be learned today. Immediately answer this question: “Why do we need copy constructor?”.

Copy constructor required for, so that we can create “real” (not bitwise) copy the Object Class. This copy of the object may be required in the following cases:

  • when a class object to the transfer function, as a parameter by value (not by reference);
  • when returning from the class of the object function, as a result of her work;
  • initializing one object class further aspect of this class.

When transferring an object as a parameter to the function by value, This function will start to work with it a bit by bit copy, and not with the fields of the object. Let determined constructor and destructor. The first memory is allocated, and the second it releases. During operation function, bit by bit copy of the object pointer points to a memory address, where the original object. While, when the work function is completed – removed and the bit-wise copy of the object. At its removal is required to work a specific and destructor frees the memory, that is occupied by the original object,. The program will continue, and Shutdown, destructor work again, trying to free all the same memory segment. This will cause the program error.

Using copy constructor – a great way to get around these errors and problems. He will “real” object copy, which will have a private area of ​​the heap.

Copy constructor the syntax is as follows:

Let us examine the following simple, but a very good example. In it we will address all 3 in which case it is desirable to use copy constructor. the class will be created, containing no-argument constructor, copy constructor and destructor. For example it was not too bulky, The constructor and destructor will display the message type “Triggering designer”, “Triggering dektruktor”… To allocate and free memory will not. We will be perfectly seen how many times and how many designers will work again destructor. obviously, destructor (if he freed memory) should not trigger more times, than designer, allocating memory.


The constructor with no parameters will be called during the creation of new facilities class. Copy constructor – during the making of copies of an object. The destructor is triggered when removing and real object and its copy. The functions of the body all described in detail and does not require any additional comments.

Executing the program will see the following in the console:

copy constructor in C ++,  c ++ copy constructor,  Programming in C ++ from scratch

Let's see what program generated to the console. Block 1 – when creating a new object, load no-argument constructor. At block 2 we have placed the function showFunc(). During transmission in it “object parameter” by value, load copy constructor and create “real” a copy of the object class OneClass. When you exit this function, load destructor, as the copy of the object is destroyed. By the way, then, that the transfer of the object as a parameter by value, causes the copy constructor, serves as a good reason to pass an object by reference. This will save both time and memory.

At block 3 posted function returnObjectFunc(). As spelled out in her body to create a new object of the class OneClass – first load the constructor without parameters. Next, the function code is executed and during the return of the object in the main function main, load copy constructor. At the end, as it should be, destructor worked twice: object and its real copy.

At fourth block, during the declaration and initialization of a new object object2, load copy constructor. Upon completion of the work load for program destructor copy of the object of the fourth block and object object1 from the first block.

If we comment out /*copy constructor * / in the classroom and run the program again – see, that the constructor without parameters work 2 fold, and destructor – It runs for five times.

copy constructor in C ++,  c ++ copy constructor, Programming in C ++ from scratch

In this situation, If the destructor frees the memory - to an error occurred in the program.

I highly recommend reading theme Copy constructor in the book by Stephen Prata “The programming language C ++. Lectures and exercises. 6-edition.” It revealed a much deeper and includes all the basic nuances of using the copy constructor. The article details the assignment operator =.

To support our site – click on the piggy bank and choose any convenient way.

Newsletter of programming:

Agree to receive notifications from to my e-mail

The copy constructor in C ++

3 thoughts on “The copy constructor in C ++

  1. And why in the code defined, unused pointer?

    1. Because at least some variable, member of the class, It should be defined in the class?
      If you do not like a pointer, define a variable of type int.

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>