About operators plus minus sign (-)

Source: Internet
Author: User

1: when using a common function

Front ++: the parameter is a non-read-only reference type, and the return type is a non-read-only reference. The value must be calculated first and then

Post ++: the parameter is a non-read-only reference type and an int type (INT type does not work to distinguish between front and back ). The returned value is a common class. The value is first taken before calculation (an unknown object can be used)

# Include <iostream> # Include <Stdio. h> # Include < String > Using   Namespace  STD;  Class  A {  Int  N;  Public : (  Int  N): n (n ){}  Int   Get  (); Friend & Operator ++ (& A); friend  Operator ++ (A &, Int  B) ;}; & Operator ++ (& A) {A. N ++ ; Return  A;}  Operator ++ (A &, Int  B ){  Return A (A. N ++ );}  Int A :: Get  (){  Return  N ;}  Int  Main () { = 5  ;( ++ (++ A) ++ ; Cout <. Get () < Endl; A B = A ++ ; Cout <B. Get () < "   " <. Get () < Endl; System (  "  Pause  "  );  Return  0  ;} 

2: function member:

Front ++: No parameter (there is an implicit this pointer pointing to the current object). The returned type is a non-read-only reference. The value must be calculated first and then

Rear ++: an int type (INT type does not have any function, used to distinguish between front and back) and another (implicit this type, pointing to the current object ). The returned value is a common class. The value is first taken before calculation (an unknown object can be used)

# Include <iostream> # Include <Stdio. h> # Include < String > Using   Namespace  STD;  Class A {  Int  N;  Public  : (  Int  N): n (n ){}  Int   Get  (); & Operator ++ ();  Operator ++ ( Int  B) ;}; & ::Operator ++ () {N ++ ;  Return * This  ;} ::  Operator ++ ( Int  B ){  Return A (n ++ );}  Int A :: Get  (){  Return N ;}  Int  Main () { = 5  ;( ++ (++ A) ++ ; Cout <. Get () < Endl; A B = A ++ ; Cout <B. Get () < "   " <. Get () <Endl; System (  "  Pause  "  );  Return   0  ;} 

3: Write with youyuan:

# Include <iostream> # Include <Stdio. h> # Include < String > Using   Namespace  STD; Class  A {  Int  N;  Public  : (  Int  N): n (n) {} friend  Int   Get  (A); & Operator ++ ();  Operator ++ ( Int  B) ;}; & :: Operator ++ () {N ++ ;  Return * This  ;} ::  Operator ++ ( Int  B ){  Return A (n ++ );}  Int   Get  (){  Return A. N ;}  Int  Main () { = 5  ;( ++ (++ A) ++ ; Cout < Get (A) < Endl; A B = A ++ ; Cout < Get (B) < "   " < Get (A) <Endl; System (  "  Pause  "  );  Return   0  ;} 

 

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.