#include <iostream>using namespace STD;classa{Private:intX Public: A (): X (0) {x =0;cout<<"Construct"<< Endl; AConstA &a) {x = a.x;cout<<"Construct copy"<< Endl; } ~a () {cout<<"Destruct"<< Endl; }//A operator () (int rhs)a&operator()(intRHS) {x + = RHS;return* This; }intGetX () {returnX }};voidMain () {{A A;cout<< A (1)(2)(3). GetX () << Endl;cout<< a.getx () << Endl; }}
The code presented in this section includes the following:
+ Reference Pass
+ Type Object Pass parameter
+ Overloaded operator Continuous invocation
In the case where the a& operator () (int RHS) function type declaration is defined, there is no new object in between, and each operation is the same object, so the final output is:
In the case of defining a operator () (int RHS) function type declaration, a new anonymous object is generated in the middle, and the copy constructor is automatically called, printing construct copy. The final output is
The trick to repeatedly invoke an overloaded operation is that the return type is a reference of the original type.
This technique can be used to implement recursive access to a path, realizing the idea of a previous blog post: http://blog.csdn.net/lonelyrains/article/details/45093007.
C + + code appreciation, such as Object transfer