Reputation: 1251
I am trying to understand the concept of copy constructor. With copy constructor I get desired result. But with out copy constructor I get the same result. The code is given here:
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
// Point(const Point &p2) {x = p2.x; y = p2.y; }
void set()
{
x=50;
y = 100;
}
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1 ;
// p2 = p1;
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
p2.set();
cout << "\np1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
// Copy constructor is called here
// Let us access values assigned by constructors
return 0;
}
The out put is :
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
p1.x = 10, p1.y = 15
p2.x = 50, p2.y = 100
Shouldn't it be :
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
p1.x = 50, p1.y = 100
p2.x = 50, p2.y = 100
Edit 1 : What if I initialize objects like this:
Point p1(10, 15); // Normal constructor is called here
Point p2(11,10);
p2 = p1;
Will it call copy constructor? If not, why in this case the result is the same?
Upvotes: 1
Views: 78
Reputation: 101
A copy constructor is called usually in cases of declaration and initialization of object to another object at same time, or while passing to functions. What the first statement means is:
Point p1(10,15);
Point p2(p1);
Is an example of usage of copy constructor while,
Point p1(10,15);
Point p2;
p2=p1;
is just plain assignment. And No. Your output is right in the first case. A copy constructor just copies the values of one object into another, it doesn't make the new one a reference object. Also, the main use of copy constructor is usually in functions. As a footnote, to understand constructors and when they are called, you could use different cout statements in them. Then you'll know when a particular constructor is called. P.S. I'm a beginner, so please tell me if I've got anything wrong.
Upvotes: 0
Reputation: 41110
C++ makes a distinction between objects and references to objects. If you want
p2
and p1
to point to the same Point
, you should do this:
Point& p2 = p1;
Because you never defined a copy constructor for your Point
class, the compiler gave you one. It will effectively look like this:
Point(const Point& other) : x(other.x), y(other.y)
{}
And your code, Point p2 = p1
will call this copy constructor.
This causes the integer values to be copied, not point to the same location in memory.
Upvotes: 2
Reputation: 726779
But without copy constructor I get the same result.
This is because you get a default copy constructor and a default assignment operator inserted for you by the compiler. It does precisely the same thing as your hand-written copy constructor does, so there is no need to code one manually.
Shouldn't it [the optput] be ...
No, it should not. In your code p1
and p2
are different unrelated objects, even though the initial state of p2
comes from p1
.
You need to use pointers or references to get the behavior that you want:
// Point p2 = p1;
Point& p2(p1);
Now p2
behaves as an "alias" for p1
, so there is really one object.
Upvotes: 2