The line
f(::x) = h(x);
is equivalent to
operator=(f(::x), h(x));
First, one of the functions f
or h
are called and the returned value is passed to operator=
. Then the other function is called and the returned value is passed to operator=
. The order is irrelevant because in either case f
changes the value of global x
to -4
and returns a reference to it and h
changes local x
(local in main
) to 12 and returns its value. Then the value 12 is assigned to global x
.
The line
int g(int f) { return x; }
defines a function that returns the value of global x
.
Maybe using different variable names helps understanding:
#include <iostream>
using namespace std;
int globalX = -2;
int h(int &hX) {
hX = 2 * hX;
return hX;
}
int g(int f) { return globalX; } // f is unused
int &f(int &fX) {
fX += ::globalX;
return fX;
}
int main() {
int mainX = 6;
f(::globalX) = h(mainX);
cout << f(mainX) << endl;
cout << g(mainX) << endl;
cout << h(mainX) << endl;
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…