For visual observation, the following code copies the constructor to num+1 the member variable
Class B{int num;public:b (): num (0) {cout << "default constructor" << " num=" << num <<endl;} B (int n): num (n) {cout << "parameter constructor" << " num=" << num << Endl;} B (const b &b) {num = b.num+1;cout << copy constructor << num= << num << Endl;} /*b (B &&b) {cout << move constructor << Endl;} */b& operator= (const B &b) {cout << assignment operator << num= <<b. Num << endl;if (this = = &A MP;B) Return *this;num = B.num;return *this;} ~b () {cout << destructor << " num=" << num << Endl;}}; b Play (b b) {b bb (+); return BB;} void Main () {{B B1 (1); B B2 = Play (B1);} cout << "-------------------------" << Endl; {B B1; B B2 (ten); b2 = Play (B1);}}
Output:
The results show that after a function call is finished, the local variables generated by the destructor code are first destructor, and then the temporary variables generated by the input parameters are reconstructed, and then the temporary variables generated by the return values are finally reconstructed.
Modify the play function in the above code to compare:
b Play (b b) {b bb (+); return b;} void Main () {{B B1; B B2 = Play (B1);} cout << "-------------------------" << Endl; {B B1; B B2 (ten); b2 = Play (B1);}}
Output:
In the screen output, before the horizontal line, the play return value does not produce a temporary variable, but after the horizontal line has a temporary variable, why??
Temporary variables and copy constructors (ii)