#include "stdafx.h"
#include "iostream.h"
class CAT
{
int *itsAge;
public:
CAT():itsAge(new int(5)){}
//增加拷贝构造函数
CAT(CAT &c)
{
*itsAge=c.*itsAge;//把c中的itsAge指针指向的内容赋给当前对象的*itsAge
}
~CAT(){ delete itsAge;}
int GetAge() const {return *itsAge;}
void setAge(int age){*itsAge=age;}
};
void main()
{
CAT fri;
cout<<"1111111 "<<fri.GetAge()<<endl;
fri.setAge(6);
CAT boot(fri);
cout<<"1111112 "<<fri.GetAge()<<endl;
cout<<"2222221 "<<boot.GetAge()<<endl;
fri.setAge(7);
cout<<"1111113 "<<fri.GetAge()<<endl;
cout<<"2222222 "<<boot.GetAge()<<endl;
}
程序目的是通过添加拷贝构造函数来使原来的程序正常化。
编译时出错了..希望大家帮忙看看错在哪了...
万千封印
Qyouu