#include "iostream"
using namespace std;
class simplecat
{
public:
simplecat(int age,int weight){itsage=age; itsweight=weight;}
~simplecat(){cout<<" destructor "<<endl;}
int getage() const {return itsage;}
int getweight() const {return itsweight;}
private:
int itsage;
int itsweight;
};simplecat & thefunction()
{
simplecat * p = new simplecat (5,9);
cout<<p<<endl;
return *p;
}void main()
{
simplecat & rcat= thefunction();
int age=rcat.getage();
cout<<"rcat is "<<rcat.getage ()<<" years old"<<endl;
} 书上对于释放的方法说了三种,可是我没看懂,帮是说下这三中方法是怎么做的。1、main函数里的第一行声明一个 simplecat 对象,然后从函数 thefunction()按值返回该对象2、仍是在 thefunction() 中声明一个自由存储区的simple,但是让函数thefunction()返回 一个指向该对象的指针。这样使用完该对象后,调用函数可将该指针删除3、在调用函数中声明对象,然后按引用将它传递给函数thefunction()
Helenr