Here is one program taken from a textbook featuring copy constructors:
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
class point
{
private:
int x,y;
public:
point(int ox =0, int oy =0)
{
cout << " Make object" << this << endl;
cout << " Using default constructor \n";
x = ox, y = oy;
}
point(const point &p)
{
cout << " Make object" << this << endl;
cout << " Using copy constructor \n";
x = p.x, y = p.y;
}
void move(int dx, int dy);
void display();
};
point fct(point a);
int main()
{
point a(5,2);
a.display();
point b = fct (a);
b.display();
getch();
return 0;
}
void point::move(int dx, int dy)
{
x += dx, y += dy;
}
void point::display()
{
cout << "Coordinates :" << x << " " << y << "\n";
}
point fct(point a)
{
point b=a;
//b.move(2,3);
return b;
}
It should be noted that the copy constructor is of the form : point (const point &p) instead of point (point &p) (the latter is what's in the textbook, but couldn't compile so I had to switch to the first one, but still I can't understand why :( )
The textbook said that there will be 3 lines of "Using copy constructor" , corresponding to 3 calls to the copy constructor. I think the reason for that is, when you call: b = fct(a)
The function "fct" makes a copy of a (pass-by-value), therefor one call
The line : point b = a : again, a copy constructor is invoked
The return value is then copied into b (the variable in main, not the one in fct) this is 3rd one.
However upon execution, there is only 2 calls. Can anyone give me a good explaination on this?
Two copies occur because Named Return Value Optimization (NRVO) elides one of the copies.
The first copy is the one from the argument
ainmainto the parameterainfct. This occurs because thepointis taken by-value.The second copy is from
atobinside func.The copy which is elided is the one in returning
bby value. In this case,bcan be directly allocated into thebat the call site, so no copy takes place.