Constructor is called.1 //用1構造參數b Copy Constructor is called.1 //用b拷貝構造一個臨時對象,因為此時沒有對象來接受fun的返回值 Destructor is called. 1 //參數b被析構 Destructor is called. 1 //臨時對象被析構 Constructor is called.2 //用2構造參數b Copy Constructor is called.2 //用b拷貝構造t1,此時調用的是拷貝構造函數 Destructor is called. 2 //參數b被析構 Default constructor is called. //調用默認的構造函數構造t2 Constructor is called.3 //用3構造參數b Copy Constructor is called.3 //用b拷貝構造一個臨時對象 Destructor is called. 3 //參數b被析構 The operator "= " is called.3 //調用=操作符初始化t2,此時調用的是賦值操作符 Destructor is called. 3 //臨時對象被析構 Destructor is called. 3 //t2被析構 Destructor is called. 2 //t1被析構 請按任意鍵繼續. . .
另外: B t1 = fun(2); 和 B t2; t2 = fun(3); 調用的構造函數不同,前面調用的是拷貝構造函數,后面的調用的是“=”操作符的重載,誰能告訴我原因呢 ?