C++类的临时对象和复制构造函数调用的问题
#include <stdio.h>
#include <iostream>
using namespace std;
class B
{
public:
B()
{
cout << "default constructor" << endl;
}
~B()
{
cout << "destructed" << endl;
}
B(int i):data(i)
{
cout << "constructed by parameter " << data << endl;
}
B(B &b)
{
cout << "copy constructor" << endl;
data = b.data;
}
private:
int data;
};
B play(B b)
{
return b;
}
int main(void)
{
B t1 = play(5);
return 0;
}
当有析构函数的时候调用一次复制构造函数,当没有析构函数的时候调用两次复制构造函数,这是为什么?