C + + NetEase cloud Classroom development Engineer-Operator overloading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.