C++中函数返回后的析构顺序以及时机(与调用函数下一指令的先后关系)

[复制链接]
查看11 | 回复1 | 2011-3-29 17:15:01 | 显示全部楼层 |阅读模式
#include
using namespace std;
class Cat
{
public:
Cat()
{this->itsWeight=5;cout<<"Cat constructor\n";}
Cat(Cat&) {cout<<"Cat copy constructor\n";}
~Cat()
{cout<<"Cat destructor\n";}
int itsWeight;
}
;
Cat Function(Cat);
int main()
{
Cat Mimi;
cout<<(Function(Mimi)).itsWeight<<" Now in main\n"

<<Mimi.itsWeight<<endl;
return 0;
}
Cat Function(Cat Huahua)
{
cout<<"Calling Function\n";
return Huahua;
}
高手帮忙解释一下三个“Cat destructor”所对应的析构对象,以及析构的时机。
不胜感激
拷贝的参数和返回值究竟谁先析构?

回复

使用道具 举报

千问 | 2011-3-29 17:15:01 | 显示全部楼层
总共产生3个对象,Mimi,Function的形参Huahua,Function的返回值Huahua。第一个先析构的是Function的形参Huahua,return之后就析构。第二个是Function的返回值Huahua,cout语句执行完之后就析构。第三个是Mimi,main函数return之后析构。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行