#include <iostream>using namespace Std;class x{public:x (int ii=0); void modify (); X f8 (); ~x ();x& operator= ( Const x &x1);p rivate:int i;}; x::x (int ii/* =0 */): I (ii) {cout<< "x (): " <<THIS<<ENDL;} X::~x () {cout<< "~x ():" <<this<<endl;} x& x::operator= (const x &x1) {cout<< "= ():" <<this<<endl;if (&x1! = this) { i = x1.i;} return *this;} void X::modify () {cout<< "Modify: " <<this<<endl;i++;} x f5 () {return x ();} Const x f6 () {return x ();//Returns an unnamed temporary variable, the temporary amount becomes constant}void F7 (x &xx) {xx.modify ();} x x::f8 () {cout<< "F8 ():" <<this<<endl;return x ();} void Main () {x a (1); F5 () = A;f5 (). modify ();//Just modify the temporary object, and do not modify F7 (F5 ());//f6 () = x (1);//The const returned cannot be an lvalue//f6 (). modify (); F7 (f6 ());//f7 () is a reference to the address of a temporary object that needs to be referenced by a f6 (), so the temporary object is modified so that the compiler resets the temporary object to const, which is not called F7 (F5 (). F8 ());}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Temporary amount of C + + in idle thinking