The C ++ programming language can be seen as an upgraded version of the C language. It can support all functions in the C language and greatly improve in other aspects. Among them, in the C ++ operator overload ++, -- it must be noted that ++ (--) is before the operand or after the operand, the difference is as follows:
The C ++ operator's heavy load code has been tested correctly (at least I have no problem here ^_^)
- # Include <iostream>
- # Include <cstdlib>
- Using namespace std;
- Template <typename T> class
- {
- Public:
- A (): m _ (0 ){
- }
- // +
- Const T operator + (const T & rhs)
- {
- // Need to be retried red, but see it is only a demo
- Return (this-> m _ + rhs );
- }
- //-
- Const T operator-(const T & rhs ){
- // Need to be retried red, but see it is only a demo
- Return (this-> m _-rhs );
- }
- T getM (){
- Return m _;
- }
- // ++ Indicates the previous mode. Here, A reference is returned, allowing ++
- A & operator ++ (){
- (This-> m _) ++;
- Return * this;
- }
- // ++ Returns A new A type variable and cannot be changed.
- // The purpose is to prevent A ++ attacks
- Const A operator ++ (int ){
- A <T> B = * this;
- (This-> m _) ++;
- Return B;
- }
- Private:
- T m _;
- };
- Int main (void ){
- Int I = 0;
- Cout <++ + I <endl;
- // I ++ is not allowed
- A <int>;
- A <int> B = ++;
- Cout <B. getM () <endl;
- A <int> c = a ++;
- Cout <c. getM () <endl;
- Cout <a. getM () <endl;
- Int t = a + 2;
- Cout <t <endl;
- System ("pause ");
- Return 0;
- }
The preceding section describes how to overload the C ++ operator.