C + + overloaded Post + + + and--

Source: Internet
Author: User

The C + + language does not require that the increment and decrement operators be members of the class, but because they change exactly the state of the object being manipulated, it is recommended that they be set as member functions. (but the following code is implemented in the form of a member function and a global function, for practice)

Amateur implementation Code:

1#include <iostream>2 using namespacestd;3 classTest {4Friend Test &operator--(Test &obj);5Friend Testoperator--(Test &obj,int);6  Public:7Test (intA =0,intb =0)8     {9          This->a =A;Ten          This->b =b; One     } A     voiddisplay () -     { -cout <<"A:"<< a <<"B:"<< b <<Endl; the     } -  Public: -     //Front + + -Test &operator++() +     { -          This->a++; +          This->b++; A         return* This; at     } -     //Rear-facing + + -Testoperator++(int) -     { -Test TEMP = * This; -          This->a++; in          This->b++; -         returntemp; to     } + Private: -     intA; the     intb; * }; $ //Front--Panax NotoginsengTest &operator--(Test &obj) - { theobj.a--; +obj.b--; A     returnobj; the } +  - //Rear-- $Testoperator--(Test &obj,int) $ { -Test temp =obj; -obj.a--; theobj.b--; -     returntemp;Wuyi } the intMain () - { WuTest T1 (1,2); - T1.display (); About++T1; $ T1.display (); ---T1; - T1.display (); -Test T2 (3,4); A T2.display (); +t2++; the T2.display (); -t2--; $ T2.display (); thecout <<"Hello world!\n"; the     return 0; the}

Note:

The post-build accepts an extra parameter (not used) for an int type parameter (which must be of type int). When we use the post operator, the compiler provides an argument with a value of 0 for the parameter. Although it is syntactically possible to use this extra parameter for the Post function, it is not usually done in the actual process. The only function of this parameter is to differentiate between the pre-and post-version functions, rather than to actually participate in the implementation of the post-build. Because the increment or decrement function prototypes of the predecessor and the backend versions must be distinguished by a special one, the C + + language chooses to add a placeholder parameter to the post-built version.

If we want to call the backend version through a function, you must pass a value for its shaping parameters.

eg

a.operator++ (0);//Call the post-built version, tested, no 0 is OK, as long as you can convert to an arbitrary number of int.

a.operator++ ();//Call the predecessor version

Professional Software Engineer Implementation method:

1#include <iostream>2 using namespacestd;3 classTest {4Friend Test &operator--(Test &obj);5Friend Testoperator--(Test &obj,int);6  Public:7Test (intA =0,intb =0)8     {9          This->a =A;Ten          This->b =b; One     } A     voiddisplay () -     { -cout <<"A:"<< a <<"B:"<< b <<Endl; the     } -  Public: -     //Front + + -Test &operator++() +     { -          This->a++; +          This->b++; A         return* This; at     } -     //Rear-facing + + -Testoperator++(int) -     { -Test TEMP = * This; -++* This; in         returntemp; -     } to Private: +     intA; -     intb; the }; * //Front-- $Test &operator--(Test &obj)Panax Notoginseng { -obj.a--; theobj.b--; +     returnobj; A } the  + //Rear-- -Testoperator--(Test &obj,int) $ { $Test temp =obj; ---obj; -     returntemp; the } - intMain ()Wuyi { theTest T1 (1,2); - T1.display (); Wu++T1; - T1.display (); About--T1; $ T1.display (); -Test T2 (3,4); - T2.display (); -t2++; A T2.display (); +t2--; the T2.display (); -cout <<"Hello world!\n"; $     return 0; the}

The same idea in = = and! = also used (good at using existing resources) ^_^.

C + + overloaded Post + + + and--

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.