1. Operator overloading, (you can use member functions, or you can use non-member functions) this
all of the member functions hide a parameter , this.
This binds the caller to each other.
complex c1,c2; for the addition of two complex numbers, add the left side to the right.
Inline complex&
Complex::operator + = (this, const complex& r) {This is usually a hidden
Return _DOAPL (this, r);
}
2.return by reference Syntax analysis
Inline Complex&
_DOAPL (complex* ths, const complex& r) {
return *ths;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Inline complex& This A part can have a return type, or it can not use a return type.
Complex::operator + = (Const complex& r) {However, if you want to use continuous addition, you will no longer be able to use an example : C3 + = C2 + = C1;
Return __DOAPL (THIS,R);
}
3. Operator overloading of non-member functions
Inline complex This Why not use references in a place?
operator + (const complex& x, const complex& y) {
Return complex (real (x) + real (y), imag (x) + imag (y));
}
~~~~~~~~ Why not return a reference? ~~~~~~~~~
Because the returned thing is location, is the temporary object.
TypeName () complex () creates a temporary object, Important!
C + + NetEase cloud Classroom development Engineer-Operator overloading