class X {
public:
X () : myID(id) {}
X (const X& right) : myID(++id) {}
friend ostream& operator<< (ostream&, const X&);
private:
static int id;
int myID;
};
int X::id = 0;
ostream& operator<< (ostream& os, const X& x) {
return os<<x.myID;
}
X f (X x1, X& x2) {
x1 = x2;
return x2;
}
void main () {
X x1, x2 = x1;
X x3 = f(x1,x2);
cout<<x1<<','<<x2<<','<<x3;
}
What I don't understand is how the 23. line of program works (3rd line of main() function). I know that it should use a copy constructor, and it does it at first,but after finishing copy constructor function it calls the f function and after that initializes x3 with the returned value of function f. Why does it call function f at all after copy constructor? Isn't it supposed to call only copy constructor?
What i'm trying to say is that I don't understand how copy constructor works when I have a function on the right side... Please help.