C + + Object-oriented advanced programming (II.)

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

For the next few articles, I'll recall the basics of C + +.

C + + consists of two parts 1.c++ language 2.c++ standard library This article mainly shares my notes on learning C + + language.

This time I recall the operator overloading.

First look at a piece of code, followed by I will introduce the original

#include <iostream>classcomplex{ Public: Complex (DoubleR =0,Doublei =0): Re (r), Im (i) {} complex&operator+= (Constcomplex&R); DoubleReal ()Const{returnre;} DoubleImag ()Const{returnim;} voidRealDoubleR); Private:    DoubleRe,im; Friend Complex& __DOAPL (complex*,Constcomplex&);};inlineDoubleImag (Constcomplex&x) {    returnX.imag ();} InlineDoubleReal (Constcomplex&x) {    returnx.real ();}

Knowledge point 1. Overloaded member functions

Inline complex&complex::operator + = (const complex& r) {     return __DOAPL (this, r);}

C + + calls are all started from the left, and the following calls

Complex C1 (1,2); complex C2 (2,3+ = C2;

For example C1 + = C2 His full meaning should be c1 called + = This function passes the parameter is two one is this (C1), the other parameter is the right value (C2), [in the compiler do not write, compile will error]

// The recognize member function has a this point that points to the caller // the complete form of + = should be so, who calls this function who is thisinline complex&complex::operator+ = (This ,const complex& r) {    return __dopal (this, r);}

Knowledge Point 2. Return by value, return by reference

// 2.return by reference // Reference Reception Improve efficiency There is also an important point of knowledge for callers to call C3 + = C2 + = C1; Const complex& r) {    ths->re + = r.re;    ths->im + = r.im    ; return // The object is returned, but the receive is a reference,   which is an important point of knowledge in C + + and is passed without knowing what the recipient is receiving }

Why use a reference to receive can let the caller call C3 + = C2 + = C1;

If you do not receive the reference, then the first time you call the C3 + = C2 is a temporary variable, then c3 + = C2 will be meaningless at the time of the C1 call.

Knowledge point 3 overloading non-member functions

//3. Operator overloading non-member functions without thisInline Complexoperator+ (Constcomplex& x,Constcomplex&y) {    returnComplex (real (x) +Real (y), imag (x)+imag (y));} Inline Complexoperator+ (Constcomplex& x,Doubley) {    returnComplex (real (x) +y, Imag (x));} Inline Complexoperator+ (DoubleXConstcomplex&y) {    returnComplex (x +Real (y), imag (y));}

Temporary object: TypeName () creates a temporary object why the above three return values are not reference because they must return a local variable

Knowledge point 2. is left = left + right left is always there

Knowledge point 4 overloaded operator

//<< overloading because this operator does not recognize our newly created object, we need to reload//Never write this operator as a member function must be written as a globalStd::ostream&operator<< (std::ostream& OS,Constcomplex& x)//the OS here is cout in fact cout is a class that is referenced by a const modifier in order to be able to invoke the corresponding successive{    returnOS <<'('<< Real (x) <<','<< imag (x) <<')';}

Summarize

1. Delivery without knowing what the recipient is receiving

2. The recipient (return value) is by value or by reference, and by value generally returns the temporary variable and the object that will be created, by reference is generally modified on an already existing object

3. Operator overloading do not add const, do not declare member functions

If there is an incorrect place, please correct me.

Reference << Houtie C + + Object-oriented advanced programming >>

C + + Object-oriented advanced programming (II.)

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.