Correct way to define copy constructor [duplicate]

This question already has an answer here:

is this a correct way to define the copy constructor?

this is the header of the class:

class Line{

private:
        int* iptr;
public:
        Line();
        Line(int);
        Line(const Line& obj);

        int getLength();
        void setLength(int);
};

here is copy constructor:

Line::Line(const Line &obj)//copy constructor
{
    std::cout << "Copy constructor is called" << std::endl;
    *this = obj; 
}

assigment of line1 object to line2 object:

Line line1(50);
Line line2 = line1; //assignment


Source: c++

Leave a Reply