enter Useless(k, ch) this=0x7fffb5ade0f0, ct=1, pc=(10, 0x23f7010, xxxxxxxxxx leave Useless(k, ch) enter operator+(const &) this=0x7fffb5ade0f0, ct=1, pc=(10, 0x23f7010, xxxxxxxxxx this=0x7fffb5ade0f0, ct=1, pc=(10, 0x23f7010, xxxxxxxxxx enter Useless(k) this=0x7fffb5ade100, ct=2, pc=(20, 0x23f7030, leave Useless(k) temp: this=0x7fffb5ade100, ct=2, pc=(20, 0x23f7030, xxxxxxxxxxxxxxxxxxxx leave operator+(const &) object one: this=0x7fffb5ade0f0, ct=2, pc=(10, 0x23f7010, xxxxxxxxxx object two: this=0x7fffb5ade100, ct=2, pc=(20, 0x23f7030, xxxxxxxxxxxxxxxxxxxx //"Useless two = one +one;" //首先調用"operator+(const &)",在這個函數內調用"Useless(k)"產生temp對象。 //返回時調用拷貝建構函式產生一個臨時匿名對象。 //析構temp對象。 //然後再調用移動拷貝建構函式,產生對象two。 //析構臨時匿名對象。 //當前gcc版本是g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4,對編譯過程做了改進,直接把temp對象和two對象當成一個對象,省去了後面四步。 enter Useless() this=0x7fffb5ade110, ct=3, pc=(0, 0, (object empty) leave Useless() enter Useless() this=0x7fffb5ade120, ct=4, pc=(0, 0, (object empty) leave Useless() three = one enter operator=(const &) this=0x7fffb5ade110, ct=4, pc=(0, 0, (object empty) this=0x7fffb5ade0f0, ct=4, pc=(10, 0x23f7010, xxxxxxxxxx this=0x7fffb5ade110, ct=4, pc=(10, 0x23f7050, xxxxxxxxxx this=0x7fffb5ade0f0, ct=4, pc=(10, 0x23f7010, xxxxxxxxxx leave operator=(const &) now object three: this=0x7fffb5ade110, ct=4, pc=(10, 0x23f7050, xxxxxxxxxx and object one: this=0x7fffb5ade0f0, ct=4, pc=(10, 0x23f7010, xxxxxxxxxx four = one + two enter operator+(const &) this=0x7fffb5ade0f0, ct=4, pc=(10, 0x23f7010, xxxxxxxxxx this=0x7fffb5ade100, ct=4, pc=(20, 0x23f7030, xxxxxxxxxxxxxxxxxxxx enter Useless(k) this=0x7fffb5ade130, ct=5, pc=(30, 0x23f7070, leave Useless(k) temp: this=0x7fffb5ade130, ct=5, pc=(30, 0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx leave operator+(const &) //"four = one + two"首先調用"operator+(const &)"函數。在這個函數內產生temp對象。 //在返回"operator+(const &)"函數後,並沒有產生一個臨時匿名對象,也沒有析構temp對象,而是直接以temp做參數調用移動拷貝函數"operator=(&&)"。 enter operator=(&&) this=0x7fffb5ade120, ct=5, pc=(0, 0, (object empty) this=0x7fffb5ade130, ct=5, pc=(30, 0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx this=0x7fffb5ade120, ct=5, pc=(30, 0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx this=0x7fffb5ade130, ct=5, pc=(0, 0, (object empty) leave operator=(&&) //在返回"operator=(&&)"函數後才析構temp對象。 enter ~Useless() this=0x7fffb5ade130, ct=5, pc=(0, 0, (object empty) leave ~Useless() now object four: this=0x7fffb5ade120, ct=4, pc=(30, 0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx four = move(one) //"four = std::move(one);"強行調用移動賦值函數"operator=(&&)"。 //調用之後,four對象接管了one對象的內部資源(pc和n),one對象沒有被析構,但內部資源被“掏空”了! enter operator=(&&) this=0x7fffb5ade120, ct=4, pc=(30, 0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx this=0x7fffb5ade0f0, ct=4, pc=(10, 0x23f7010, xxxxxxxxxx this=0x7fffb5ade120, ct=4, pc=(10, 0x23f7010, xxxxxxxxxx this=0x7fffb5ade0f0, ct=4, pc=(0, 0, (object empty) leave operator=(&&) now object four: this=0x7fffb5ade120, ct=4, pc=(10, 0x23f7010, xxxxxxxxxx and object one: this=0x7fffb5ade0f0, ct=4, pc=(0, 0, (object empty) //退出"main()"時,析構棧空間的對象。析構順序與構造順序相反。 enter ~Useless() this=0x7fffb5ade120, ct=4, pc=(10, 0x23f7010, xxxxxxxxxx leave ~Useless() enter ~Useless() this=0x7fffb5ade110, ct=3, pc=(10, 0x23f7050, xxxxxxxxxx leave ~Useless() enter ~Useless() this=0x7fffb5ade100, ct=2, pc=(20, 0x23f7030, xxxxxxxxxxxxxxxxxxxx leave ~Useless() enter ~Useless() this=0x7fffb5ade0f0, ct=1, pc=(0, 0, (object empty) leave ~Useless() |