Writing a Good Constructor

Written by Raza on. Posted in C++

Working with pointers in C requires a lot of practice to avoid memory leaks and dangling pointers. We discussed them in length in the previous post, Pointer Hazards in C . In object oriented paradigm, whenever you declare an object of user defined class type, a special function known as constructor is automatically invoked. Similarly when you want to copy an object to another object, copy constructor gets invoked. Writing an efficient and error free constructor is essential to the construction of a good program. In this post I shall explain how to write a good and efficient constructor and a copy constructor, paying special attention to pointer hazards.

A constructor without any parameter is known as default constructor. On the other hand, a copy constructor takes an object of the class type as a parameter. Suppose you have a class by the name of Vector which is declared as follows.

</p>
<p>Class Vector</p>
<p>{</p>
<p>&nbsp; &nbsp; private:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; int *arr ; &nbsp; &nbsp;//Array of integers</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; int size ; &nbsp; &nbsp;//Size of the Array</p>
<p>&nbsp; &nbsp; public:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; Vector () ; &nbsp; &nbsp;//Default Constructor</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; Vector (const Vector &obj) ; &nbsp; &nbsp;//Copy Constructor</p>
<p>} ;</p>
<p>

When you declare an object of this class like this, default constructor is called.

</p>
<p>Vector v1 ;</p>
<p>

When this object is created, its two data members are also declared, though not initialized. In the variable size, some garbage value is lying whereas the pointer arr is a bad pointer. These data members if used in this invalid state casino online could create many problems during program execution. To avoid this state, a suitably equipped constructor should be written. The default constructor for this class should look like this.

</p>
<p>Vector :: Vector ()</p>
<p>{</p>
<p>&nbsp; &nbsp; arr = nullptr ;</p>
<p>&nbsp; &nbsp; size = 0 ;</p>
<p>}</p>
<p>

Now it is pretty safe to use. When you declare an object like this.

</p>
<p>Vector v2 = v1 ;</p>
<p>

The copy constructor gets called in this instance. Remember that normally a copy constructor gets called in the following three conditions.

  1. Initialization of an object.
  2. Passing an object by value to a function.
  3. Returning an object by value from a function.

Anyways, when a copy constructor gets called here and you haven’t written it yourself, compiler will itself generate and execute its body. But in that compiler generated copy constructor, the copy of the data members would be done like this.

</p>
<p>v2.arr = v1. arr ;</p>
<p>v2.size = v1. size ;</p>
<p>

Notice that both the pointers now point towards the same location. If you edit or delete either of them, that change would appear in both the objects. So we want the pointers of both objects to be distinct from each other and on separate memory locations holding independent values. In order to do this, write the copy constructor explicitly yourself like this.

</p>
<p>Vector :: <a style="font :inherit; box-shadow :none; text-decoration:none;color:inherit;  outline:none" href="http://thespyexpert.com/">http://thespyexpert.com/</a> Vector (const Vector &obj)</p>
<p>{</p>
<p>&nbsp; &nbsp; size = obj.size ;</p>
<p>&nbsp; &nbsp; arr = new int [size] ;</p>
<p>&nbsp; &nbsp; for (int i = 0 ; i < size ; i)</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; arr [i] = obj.arr [i] ;</p>
<p>}</p>
<p>

This transfer of data from one object to another is known as deep copy.

Next time I would come with another similar technique or maybe some other thing. Good Bye!

Tags: , , ,