The basics of programming in c++ for beginners

The copy constructor in C ++

When 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 parameterby 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 functionby 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.

Example:

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 both the real object and its copy are deleted. In the body of functions everything is described in detail and does not require 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 functionshowFunc(). During transmission in it “object parameter” by value, load copy constructor and create “real” a copy of the object classOneClass.

When you exit this function, load destructor, since the copy of the object is being 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 functionreturnObjectFunc(). 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, loadcopy 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 =.

6 thoughts on “The copy constructor in C ++

  1. And why in the code defined, unused pointer?
    int *ptr; // какой-то указатель

    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.

  2. In the 3rd block copy constructor is not invoked, respectively and 2nd no destructor. This is for MinGW compilers, CLang(online compiler ). For microsoft vc ++ all works, as in Example.

    1. Because there really is no object, in which to return the result.
      We need to record something like that:

      OneClass object2( returnObjectFunc() );

      Or so:

      OneClass object2 = returnObjectFunc();

Leave a Reply

Your email address will not be published. Required fields are marked *