1. 程式人生 > >C++網易雲課堂開發工程師-操作符重載

C++網易雲課堂開發工程師-操作符重載

操作 () 返回 urn lin per spa 加法 操作符

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() 創建臨時的對象,重要!

C++網易雲課堂開發工程師-操作符重載