以下例子构造函数将被调用一次,复制构造函数将被调用四次。
#include "iostream"
using namespace std;
class CA
{
public:
CA(){ cout<< "construct be called." << endl;}
CA(CA& ra){ cout << "copy construct be called. "<< endl;}
};
CA fun(CA a)
{
CA na(a);
return na;
}
int main(int argc, char* argv[])
{
CA fa;
CA fb = fun(fa);
return 0;
}
输出结果:
construct be called
copy construct be called
copy construct be called
copy construct be called
copy construct be called