Implements the overload of the increment (decrease) operator between the front and the rear.

Source: Internet
Author: User

Assume that classa is overloaded with front and back ++, the form of the overload function should be as follows:

Classa & classa: Operator ++ () // front ++
{
Itsval ++;
Return * this;
}
Const classa: Operator ++ (INT) // post ++
{
Classa temp (* This );
Itsval ++;
Return temp;
}

Several of them can be studied:

1> to distinguish between the prefix and Postfix operators, you need to add the parameter "int" to the Postfix operator overload function, although this type does not represent any actual meaning except for the difference;

2> the prefix returns a reference to the variable, and the latter returns a constant. Therefore, ++ C is legal, while C ++ is invalid. This can be verified by using the basic data type ++ operation in VC 6.0 and Dev C ++;

3> Why not let C ++ be legal? If you want to implement C ++, you must make the post-return variable or variable reference. C ++ returns the C value first and then + 1, so it is impossible to return C, so we can only create local variables to save the initial values of C, then, return local variables (local variables do not allow returning references), but after returning local variables, if the next ++ operation is performed, the value of this local variable is involved in the calculation. Therefore, C ++ is equivalent to C ++ at this time, so it has no significance.

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.