Thinking of C + + increment (increment) operator overloading

Source: Internet
Author: User

In the previous chapters we have been exposed to the overload of the increment operator, at that time we did not distinguish between the previous increment and the ++a difference, under normal circumstances we are not separate from the a++, but it is true that there is a clear difference between them.

First look at the following code:

#include <iostream>
using namespace Std;

int main ()
{
int a=0;
+ + (++a)/correct, (++a) returns the left value
(a++) ++;//error, (a++) return is not a left value
System ("pause");
}

In code (a++) + + Compile error, return "+ +" needs the error of the left value, which is the difference between the previous increment and after increment, then why?

The reason is mainly caused by the definition of C + + for the increment (increment) operator.

The difference between them is mainly the following two points:

1, the operation of the process, the object will be incrementally modified, and then return the object (in fact, is the object of reference) is called before increment (increment) operation. Written in operator overloaded functions in the form of a return object reference.

2, the operation process, first return the value of the original object, and then the object increment operation called after increment (increment) operation. Written in the operator overloaded function in the form of a value return (this is also the reason for the previous (a++) + + error. (a++) Return is not a reference, not as a left value to continue to participate in the external extension of the + + operation), the internal implementation of overloaded functions must create an object to temporarily store the value of the original object, When the function returns, the temporary object is returned.

So how do we distinguish between the pre-increment operator overload function and the post-increment operator overload function when writing operator overloaded functions?

The method is to add an int identifier to the parameter of the post increment operator overloaded function, marked as a post increment operator overloaded function.

See the following examples (example one is a non-member mode, case two is a member mode):

Example One

//Program Author: Junning 
//site: www.cndev-lab.com
//All manuscripts have copyright, if you want to reprint, please be sure to famous source and author
#include <iostream>
using namespace Std;
Class Test
{
Public:
Test (int a=0)
{
Test::a = A;

Friend test& operator + + (test&);
Friend Test operator + + (test&,int);
Public:
int A;
};
test& operator + + (Test &temp)/before increment
{
temp.a++;
return temp;
}
Test operator + + (Test &temp,int)/after increment, int here only distinguishes, in fact, no actual effect
{
Test rtemp (temp); The copy constructor is called here to replicate the object
temp.a++;
return rtemp;
}
int main ()
{
Test A (a);
+ + (++a);
cout<<a.a<<endl;
cout<< to temporarily store the value status of an object when observed incrementally: "<< (a++). a<<endl;//Here is the embodiment after the increment operation first returns the original object value Place
Cout<<a.a <<endl;
(a++) + +;
cout<<a.a<<endl;//is a value return state because it is incremented, so (a++) + + does an incremental operation on a, followed by 104 rather than 105.
System ("pause");
}

Related Article

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.