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); 调用的构造函数不同,前面调用的是拷贝构造函数,后面的调用的是“=”操作符的重载,谁能告诉我原因呢 ?