有谁能给下面的c++程序增加说明,我实在有几个地方看不懂?

来源:百度知道 编辑:UC知道 时间:2024/06/02 04:39:01
#include<iostream.h>
class A
{
public:
A(int a,int j) {this->a=a;this->b=j;}
void copy(A &aa);
void print() {cout<<a<<","<<b<<endl;;}
private:
int a,b;
};
void A::copy(A &aa)
{
if (this==&aa) return;
*this=aa;
}
void main()
{
A a1(0,0), a2(3,4);
a1.copy(a2);
a1.print();
}
重点是后面这几句,从定义copy 函数开始.本程序运行结果为3,4

void A::copy(A &aa)
{
if (this==&aa) return; //如果this和&aa指的是同一个对象,就返回
*this=aa; //从aa拷贝赋值给*this对象
}

楼上说对了,不过好像没人会写这种拷贝函数.

很少见~~~