求助:关于结构体 重载赋值运算符

来源:百度知道 编辑:UC知道 时间:2024/06/18 09:29:03
定义复数结构体:
typedef struct
{
double doux;
double douy;
}Complex;
和类:
class ComLinePowerArry
{
public:
CArray<Complex,Complex&> gComLinePowerArry;
//ComLinePowerArry operator=(ComLinePowerArry comLine){}
};

怎样定义关于ComLinePowerArry的赋值运算符?实现以下操作:
ComLinePowerArry a;
ComLinePowerArry b = a;
急!!!!!
这个?
ComLinePowerArry& operator=(ComLinePowerArry& comLine)
{
if (this->gComLinePowerArry.GetUpperBound()!= -1)
{
this->gComLinePowerArry.RemoveAll();
this->gComLinePowerArry.FreeExtra();
}
int maxup = comLine.gComLinePowerArry.GetUpperBound();
Complex temp;
for (int i=0; i<maxup+1; i++)
{
temp.doux = comLine.gComLinePowerArry[i].doux;
temp.douy = comLine.gComLinePowerArry[i].douy;
this->gComLinePowerArry.Add(temp);
}
return *this;
};

设一个Complex类型的临时变量temp.
1.清空自身的gComLinePowerArry
2.用叠代器遍历comLine
3.对comLine的每个元素,让temp等于它,然后将temp加进gComLinePowerArry

另外,这个重载函数的声明写的有点问题,应该是
lweiyue写的那样.

ComLinePowerArry &operator=(const ComLinePowerArry &other)
{
}