C++. Classes. Part 2. Copy constructor. Examples of using. Passing a class object to a function. Returning a class from a function

C++. Classes. Part 2. Copy constructor. Examples of using. Passing a class object to a function. Returning a class from a function

In this topic, the work of the copy constructor is considered using the example of unmanaged (native) classes. When considering this topic, it is recommended to read the topic:


Contents


1. What is the purpose of a copy constructor? Example

This is a special constructor which allows you to get the identical object. That is, using the copy constructor, you can get a copy of an existing object. The copy constructor is also called a copy initializer. The copy constructor should receive the reference (&) to the object of same class. This reference is as input parameter.

The copy constructor is called when the object of class is initialized when declared.

An example. Let the class CMyPoint, describing a point on the coordinate plane, be given. The class declares several constructors, including the copy constructor.

// class CMyPoint
class CMyPoint
{
    int x, y;

    public:
    CMyPoint(void); // default constructor
    CMyPoint(int nx, int ny); // constructor with two parameters
    CMyPoint(CMyPoint & ref_Point); // copy constructor

    // Access methods - implemented in the class
    int GetX(void) { return x; }
    int GetY(void) { return y; }
};

// implementation of class constructors (methods)
// CMyPoint class default constructor
CMyPoint::CMyPoint(void)
{
    x = y = 0;
}

// a CMyPoint class constructor with two parameters
CMyPoint::CMyPoint(int nx, int ny)
{
    x = nx;
    y = ny;
}

// copy constructor of CMyPoint class
// a reference to CMyPoint is passed
CMyPoint::CMyPoint(CMyPoint & ref_Point)
{
    // copying data from one object into another
    x = ref_Point.x;
    y = ref_Point.y;
}

Demonstration of the use of the copy constructor in some program code (method)

// demonstrating the use of the copy constructor
CMyPoint p1(5, 8); // creating object p1, the constructor with two parameters is called
CMyPoint p2; // creating object p2 - the default constructor is called

// testing
int d;
d = p1.GetX(); // d = 5
d = p2.GetX(); // d = 0 

p2 = p1; // bitwise copying, the copy constructor is not called
d = p2.GetX(); // d = 5 - Data were copied

// a code, that calls a copy constructor
CMyPoint p3 = p1; // object initializing => the copy constructor is called
d = p3.GetX(); // d = 5
2. When is it useful to use the copy constructor?

The copy constructors are very important. In the class, the copy constructor must be declared in cases, if necessary:

  • create a copy of the class object (see the previous paragraph);
  • pass the object of the class to some function as a parameter-value (see the example below);
  • if you need to return a class object by value.

 

3. Example of passing a class object to a function as a parameter-value

In the example, an object of the CMyPoint class (see paragraph 1) is passed to the GetLength() function, which calculates the distance from the CMyPoint point to the origin. The function text is as follows:

// function that calculates the distance from the point to the origin
// point is an incoming parameter
double GetLength(CMyPoint mp)
{
    double length;
    int tx, ty;

    tx = mp.GetX();
    ty = mp.GetY();
    length = Math::Sqrt(tx*tx + ty*ty);

    return length;
}

Using a function in another program code

CMyPoint p1(5,5); // declare an instance of the CMyPoint class
double len;

// pass the point p1 to the function, the copy constructor is called, len = 7,07...
len = GetLength(p1);

 

4. Example of returning a class object from a function by value using the copy constructor

Implement the GetCenterPoint() function, which returns a point that is the midpoint of the segment drawn between the CMyPoint point and the origin.

The class declaration is exactly the same as in paragraph 1.

Implementation of two variants of functions GetCenterPoint() and GetCenterPoint2().

// function that returns the middle of a line segment
CMyPoint GetCenterPoint(CMyPoint mp)
{
    int tx, ty;
    tx = mp.GetX() / 2;
    ty = mp.GetY() / 2;

    // returning from the function, the copy constructor is not called,
    // the constructor with two parameters is called instead of it
    return CMyPoint(tx, ty);
}

// function that returns the midpoint of a segment specified by points
CMyPoint GetCenterPoint2(int x, int y)
{
    CMyPoint mp(x/2, y/2);

    // a temporary object, that is initialized with the value of mp, is created,
    // as a result, the copy constructor is called
    return mp;
    // in this case, the copy constructor is not called
    // return CMyPoint(x/2,y/2);
}

Demonstration of the use of functions

CMyPoint mp(18,-8);
CMyPoint mpC;

// the copy constructor is called when passing the parameter to the function as a value parameter
mpC = GetCenterPoint(mp); 

int cx, cy;
cx = mpC.GetX(); // cx = 9
cy = mpC.GetY(); // cy = -4

mpC = GetCenterPoint2(-9, 13); // the copy constructor is called when returning from function
cx = mpC.GetX(); // cx = -4
cy = mpC.GetY(); // cy = 6

In the first version of GetCenterPoint(), the copy constructor is called only when mp is passed by value. When you return from the GetCenterPoint() function using the return statement, the copy constructor is not called. Instead, it calls a constructor with two parameters declared in the class.

In the second version of GetCenterPoint2(), the copy constructor is called when the function returns a value by using return statement. In the return statement, a temporary object of the CMyPoint class is created, which is immediately initialized with the value of mp. As a result, the copy constructor is called.

 


Related topics