Remember the left and right values and access data before recording + +.
Data storage is divided into three parts, heap area, stack area and static variable area.
The left value can be changed, and the right value cannot be changed
Both the stack area and the heap are stored in an lvalue, and you can change its value arbitrarily, and the static variable area portion of the data is an rvalue, such as a const-decorated value, a temporary variable created by the function, which cannot be changed
Prefix + + overload, direct direct + + operation, return itself can
Suffix + + overloads, create temporary variables, perform +1 operations on the original element, return temporary variables, return value types with a const modifier, make the return value a right value, non-modifiable, prevent (+ + (class++)) case
1#include <bits/stdc++.h>2 using namespacestd;3 classcl{4 Public:5 intx;6 cl () {}7ClConstcl&c) {8cout<<"execute copy constructor \ n";9x=c.x;Ten } Onecl&operator++(){//prefix + + reload Ax+=3; - return* This; - } the ConstCloperator++(int){//suffix + + reload -CL MID = * This; - This->x+=2; - returnmid; + } - voidPrin () { +cout<<"cl x ="<<x<<Endl; A } at }; - - intfunc () - { - intx=1; - returnx; in } - intMain () to { + CL C1; -c1.x=0; theCL C2= (c1++); *cout<<c1.x<<" "<<c2.x<<"\ n"; $ Panax Notoginseng intA=Ten; - intx= (+ + (a++));//error, a++ generated temporary variable is the right value, cannot be changed the + return 0; A}
C + + operator overloading problem