|
发表于 2004-7-31 13:38:00
|
显示全部楼层
Re: [求助]C++高手请进
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int age);
Cat(Cat&);
~Cat();
int getage() const {return itsage;}
private:
int itsage;
};
Cat::Cat(int age)
{
itsage=age;
cout <<"构造"<<endl;
}
Cat::Cat(Cat&)
{
cout <<"复制构造"<<endl;
}
Cat::~Cat()
{
cout <<"析构"<<endl;
}
Cat& function();
int main()
{
Cat sky(100);
cout <<sky.getage()<<endl;
sky=function();
cout <<sky.getage()<<endl;
return 0;
}
Cat& function()
{
Cat asky(200);
return asky;
}
==================================
改变了Cat& function();
有一定的危险性,因为function()完了后asky已经析构,引用
析构的地址是很危险的.
================================== |
|