64,651
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class son
{
public:
int age;
son():age(0){}
son(int iAge)
{
this->age = iAge;
}
son(const son & rhs)
{
this->age = rhs.age;
}
// Operator
son & operator = ( const son & rhs);
};
son & son::operator =( const son &rhs)
{
if (this == &rhs) return *this;
return *(new son(rhs));
}
void main(void)
{
son s1(10);
son s2;
s2 = s1;
cout << "s1 age is:" << s1.age << endl;
cout << "s2 age is:" << s2.age << endl;
}// main
son & son::operator =( const son &rhs)
{
if (this == &rhs) return *this;
return *(new son(rhs));
}
son & son::operator =( const son &rhs)
{
if (this == &rhs) return *this;
this->age = rhs.age;
return *this;
}
son(const son & rhs)
{
this->age = rhs.age;
}