求教复制构造函数的问题
#include<iostream.h>class human
{
public:
static int human_num;
human()
{
human_num++;
}
~human()
{
human_num--;
print();
}
human(human &h)
{
human_num++;
cout<<"copy construction"<<endl;
}
void print()
{
cout<<"human_num is:"<<human_num<<endl;
}
};
int human::human_num=0;
human f1(human x)
{
cout<<"what"<<endl;
x.print();
return x;
}
int main()
{
human h1;
h1.print();
human h2=f1(h1);
return 0;
}
输出结果为:
human_num is:1
copy construction//f1()函数形参传递时调用的复制构造参数
what
human_num is:2
copy construction//return 时调用的复制构造参数
human_num is:2//f1()子函数临时变量的析构
human_num is:1//h2的析构
human_num is:0//h1的析构
但是如果把主函数修改为:
int main()
{
human h1;
h1.print();
f1(h1);
return 0;
}
结果还是上面的结果:这点我不明白
human_num is:1
copy construction
what
human_num is:2
copy construction
human_num is:2
human_num is:1
human_num is:0
求大侠指导,...