C++网易云课堂开发工程师-操作符重载

1.操作符重载,(可以使用成员函数,也可以使用非成员函数) this

所有的成员函数均隐藏着一个参数this.

this与调用者相互绑定。

complex c1,c2;                                                                                         对于两个复数的相加,暗含着左边加到右边。

inline complex&

complex::operator += (this, const complex& r){                                 this通常为隐藏的

  return _doapl(this, r);

}

2.return by reference 语法分析

inline complex&

_doapl(complex* ths, const complex& r){

  return *ths;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

inline complex&                                                                                                  这一部分可以拥有返回类型,同样也可以不使用返回类型。

complex::operator += (const complex& r){                                                     但是,如果要使用连续加法,那么将不再能够使用了例如:C3 += C2 += C1;

  return __doapl(this,r);

}

3.非成员函数的运算符重载

inline complex                                                                                                      这个地方为什么不使用引用呢?

operator + (const complex& x, const complex& y){

  return complex(real(x) + real(y), imag(x) + imag(y));

}

~~~~~~~~为什么不返回引用呢~~~~~~~~~

因为返回的东西,是location的,是临时对象。

typename()                       complex()                                                                    创建临时的对象,重要!

猜你喜欢

转载自www.cnblogs.com/sky-z/p/9502548.html
今日推荐