[C++] Operator overloading ⑦ (Unary operator overloading | Postfix operator overloading | Use global functions to implement postfix ++ auto-increment operator overloading | Use member functions to implement postfix — auto-decrement operator overloading)

Article directory

  • 1. Post operator overloading
    • 1. Use global functions to implement postfix + + auto-increment operator overloading
    • 2. Use member functions to implement postfix — decrement operator overloading
  • 2. Complete code example

In the previous blog [C++] operator overloading ⑥ (unary operator overloading | post-operator overloading | the difference between pre-operator overloading and post-operator overloading | post-operator overloading adds int placeholder parameters), the pre-positioning was explained The difference between operator overloading and post operator overloading,

  • Prefixed operator overloading: Student & amp; operator + + (Student & amp; s), returns an object reference, and the parameters are normal;
  • Post operator overloading: Student operator + + (Student & s, int), the returned object value, the last digit of the parameter list needs to add an int type account bit parameter ;

It can be seen that the difficulty of overloading of post-operators is higher than that of pre-operators;

1. Post operator overloading

1. Use global functions to implement postfix + + auto-increment operator overloading

Use global functions to implement postfix + + auto-increment operator overloading:

  • First, write out the function name. The function name rule is “operate” followed by the operator to be overloaded. The function name is operate + + ;
operate + +
  • Then, Write the function parameters according to the operands, The parameters are generally references to objects;
    • Since this is a unary operator overload, only this object needs to be operated, and the object itself needs to be passed in as a parameter. In addition, in order to distinguish it from the prefix operator, an int type placeholder parameter is added;
operator + + (Student & s, int)
  • Then, Perfect the return value according to the business, The return value can be a reference/pointer/element;
    • Implement the increment operation of a Student object;
    • First use the Student & s object in the parameter, and then increment it;
    • Therefore, the Student&s object needs to be auto-incremented, but the object used is the returned object, which must be an object without auto-increment;
    • Here ret is used to save the s object value, and then the ret value is returned. This is an object without auto-increment;
    • The value in the s object needs to be incremented;
Student operator + + (Student & s, int)
  • Finally, implement the function body and write specific operator operation business logic;
//Use global function to implement postfix + + auto-increment operator overloading
// Overload the postfix + + operator
// Implement the increment operation of a Student object
// First use the Student & s object in the parameter, and then increment it
// Therefore the Student & s object needs to be incremented
// But the object used is the returned object, which must be an object without self-increment.
// Use ret here to save the s object value, and then return the ret value
//The value in the s object is incremented
//Returns a new Student object
Student operator + + (Student & s, int)
{<!-- -->
Student ret = s;
s.age + + ;
s.height + + ;
return ret;
};

2. Use member functions to implement post-decrement operator overloading

Use member functions to implement post-decrement operator overloading:

  • First, write out the function name. The function name rule is “operate” followed by the operator to be overloaded. The function name is operate -- ;
operate--
  • Then, Write the function parameters according to the operands, The parameters are generally references to objects;
    • Since this is a unary operator overload, you only need to operate this object, and there is no need to pass in the object itself. You can directly use this pointer to operate the object, and only add int type placeholder parameters;
operator--(int)
  • Then, Perfect the return value according to the business, The return value can be a reference/pointer/element;
    • Implement the decrement operation of a Student object
    • First use the object pointed to by this pointer, and then decrement it;
    • Therefore, the object pointed to by this pointer needs to be decremented, but the object used is the returned object, which must be an object without decrement;
    • Here ret is used to save the object value pointed to by this pointer, and then the ret value is returned. This is the object value without decrement;
    • The value of the object pointed to by this pointer is decremented;
Student operator--(int)
  • Finally, implement the function body and write specific operator operation business logic;
public:
// Use member functions to implement postfix -- decrement operator overloading
// Overload the postfix -- operator
// Implement the decrement operation of a Student object
// First use the object pointed to by this pointer, and then decrement it
// Therefore, the object pointed to by this pointer needs to be decremented
// But the object used is the returned object, which must be an object without self-decrement.
// Use ret here to save the object value pointed to by this pointer, and then return the ret value
// The value of the object pointed to by this pointer is decremented
//Returns a new Student object
Student operator--(int)
{<!-- -->
Student ret = *this;
this->age--;
this->height--;
return ret;
};

2. Complete code example

Code example:

#include "iostream"
using namespace std;

class Student
{
public:
// Constructor with parameters, set default values for parameters
Student(int age = 1, int height = 1)
{
this->age = age;
this->height = height;
};

public:
//Print data
void print()
{
cout << "age = " << age << " , height = " << height << endl;
};

public:
// Use member functions to implement postfix -- decrement operator overloading
// Overload the postfix -- operator
// Implement the decrement operation of a Student object
// First use the object pointed to by this pointer, and then decrement it
// Therefore, the object pointed to by this pointer needs to be decremented
// But the object used is the returned object, which must be an object without self-decrement.
// Use ret here to save the object value pointed to by this pointer, and then return the ret value
// The value of the object pointed to by this pointer is decremented
//Returns a new Student object
Student operator--(int)
{<!-- -->
Student ret = *this;
this->age--;
this->height--;
return ret;
};

public:
int age; // age
int height; // height
};

// Use global function to implement prefix + + auto-increment operator overloading
// Overload the prefix + + operator
// Implement the increment operation of a Student object
// Due to changes in the attributes in Student & s in the parameter
// You still need to return the Student & s parameter itself when returning
Student & amp; operator + + (Student & amp; s)
{
s.age + + ;
s.height + + ;
return s;
};

//Use global function to implement postfix + + auto-increment operator overloading
// Overload the postfix + + operator
// Implement the increment operation of a Student object
// First use the Student & s object in the parameter, and then increment it
// Therefore the Student & s object needs to be incremented
// But the object used is the returned object, which must be an object without self-increment.
// Use ret here to save the s object value, and then return the ret value
//The value in the s object is incremented
//Returns a new Student object
Student operator + + (Student & s, int)
{<!-- -->
Student ret = s;
s.age + + ;
s.height + + ;
return ret;
};

int main() {
//Add custom types
Student s1(10, 120), s2(18, 170);
Student s3, s4, s5;

+ + s1;
s1.print();

s2++;
s2.print();

s2--;
s2.print();

    //The console pauses, press any key to continue execution backwards
    system("pause");

    return 0;
};

Execution results:

age = 11, height = 121
age = 19, height = 171
age = 18, height = 170
Please press any key to continue. . . .