Example of the c ++ overload Post-auto-increment operator

Source: Internet
Author: User
Tags error code
The code is as follows: Copy code

# Include <iostream>
Using namespace std;
Class num
{
Public:
Int x;
Num () {n = 1; cout <"constructor execution! N ";}
Num (int I) {n = I; cout <": run the constructor with parameters! N ";}
Num (const num & s) {this-> n = s. n; cout <"copy constructor execution! N ";}
~ Num () {cout <"destructor execution! N ";}
Int get () const {return n ;}
Void set (int x) {n = x ;}
Void add () {++ n;} // the error code returned when the custom addition function is incorrect: expected '; 'before': 'toke
Const num & operator ++ () {// pre-auto-increment. The key to const is to prevent auto-increment after the return, that is, to avoid the appearance of ++ and so on, to avoid copying constructor calls by referencing '&', operator is the c ++ keyword function name, which is the same as the normal number of reloads. The type of the c ++ defined operator, the following ++ indicates that the auto-increment operator is reloaded, and the implementation part is in {}.
++ N;

Return * this; // * shis is the original object
}

Const num operator ++ (int o) {// Post auto-addition. The Function of parameter o is different from the preceding function. The key of const is to prevent auto-addition after return, that is to say, to avoid the appearance of ++ and the like, Reference '&'. To avoid calling the copy constructor, operator is the c ++ keyword function name, which is the same as the normal number of reloads, the type of the defined operators in c ++. The following ++ indicates that the self-added operators are reloaded, and the implementation part is in {}.
Num tem (* this );
This-> n ++; // this-> can be omitted
Return tem;

}
Private:
Int n;
};
Int main ()
{
Num I;
// I. add (); // call the member function to implement auto-addition
Num y = I ++; // Post auto-increment
Cout <"I:" <I. get () <endl; // Post-auto-added I member value
Cout <"y:" <y. get () <endl; // Post-auto-Added Member values of y
// Y ++; this is wrong because I is an object and there is no auto-increment operation.
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.