self-confidence,the source of all the power

导航

运算符重载为友元函数

运算符重载为类的友元函数,就必须把操作数全部通过形参的方式传递给运算符重载函数。

如:

class complex

{

public:

  complex(double x,double y):real(x),imag(y) {}

  //friend complex operator + (complex c1,complex c2); //友元函数

  //friend complex operator - (complex c1,complex c2);

  complex operator +(complex c2)   //非友元函数定义

  {

    return complex(real + c2.real,imag + c2.imag);

  }

  void display():

private:

  double real;

  double imag;

};

posted on 2011-10-02 20:36  漩涡鸣人  阅读(562)  评论(0编辑  收藏  举报