Home > Back-end >  Why does the copy constructor of an object that is being used for intializing another object gets in
Why does the copy constructor of an object that is being used for intializing another object gets in

Time:10-16

class point
{
public:
    point(double x, double y) : x(x), y(y)
    {
        std::cout << "point parameterized constructor of: "
        << getThis() << std::endl;
    }

    point(const point& that) : x(that.x), y(that.y)
    {
        std::cout << "point copy constructor of: "
        << getThis() << std::endl;
    }

    ~point()
    {
        std::cout << "point destructor of: "
        << getThis() << std::endl;
    }
private:
    double x;
    double y;
    point* getThis() { return this; }
};

class line
{
public:
    line(const point& startPoint, const point& endPoint) :
    startPoint(startPoint)
    endPoint(endPoint)
    {
        std::cout << "line parameterized constructor: " << getThis() << std::endl;
    }
    ~line()
    {
        std::cout << "line destructor of: "
        << getThis() << std::endl;
    }
private:
    point startPoint;
    point endPoint;
    line* getThis() { return this; }
};

int main()
{
    point p1(3.0, 4.0);
    point p2(5.0, 6.0);

    line l1(p1, p2);

    return 0;
}

Output of the program:

point parameterized constructor of: 0x577fffc00
point parameterized constructor of: 0x577fffbe0
point copy constructor of: 0x577fffbb0
point copy constructor of: 0x577fffbc8
lineSegment parameterized constructor of: 0x577fffbb0
lineSegment destructor of: 0x577fffbb0
point destructor of: 0x577fffbc8
point destructor of: 0x577fffbb0
point destructor of: 0x577fffbe0
point destructor of: 0x577fffc00

I don't understand how point's copy constructor gets invoked 2 times (1 for each point parameter) Reason why is originally, line constructors parameters were not const references. And the compiler was giving this warning for the line constructor

Clang-Tidy: The parameter 'endPoint' is copied for each invocation but only used as a const reference; consider making it a const reference
line(point startPoint, point endPoint) : startPoint(startPoint), endPoint(endPoint) {...}

And this was the output:

point parameterized constructor: 0x8feedffa40
point parameterized constructor: 0x8feedffa20
point copy constructor: 0x8feedffa60
point copy constructor: 0x8feedffa80
point copy constructor: 0x8feedff9f0
point copy constructor: 0x8feedffa08
lineSegment parameterized constructor: 0x8feedff9f0
point destructor: 0x8feedffa80
point destructor: 0x8feedffa60
lineSegment destructor: 0x8feedff9f0
point destructor: 0x8feedffa08
point destructor: 0x8feedff9f0
point destructor: 0x8feedffa20
point destructor: 0x8feedffa40

As you can see the copy constructor of the point gets invoked 4 times(2 for each point parameter). I assumed all 4 invocations would go away when I made the parameters const point references. Instead, they halved. Why is that?

CodePudding user response:

In this constructor of the class line

line(const point& startPoint, const point& endPoint) :
startPoint(startPoint)
endPoint(endPoint)
{
    std::cout << "line parameterized constructor: " << getThis() << std::endl;
}

there is used the copy constructor of the type point for data members startPoint and endPoint in this mem-initializer list

startPoint(startPoint)
endPoint(endPoint)

In this constructor

line(point startPoint, point endPoint) : startPoint(startPoint), endPoint(endPoint) {...}

where arguments are not accepted by reference the copy constructor is called two times more to initialize the parameters.

  • Related