Original link (click the original link to get more learning dry goods):
Two class operator overload – Boolean bloghttp://blog.bools.cn/archives/1640
1. Overload of plus operator
If you want to add or subtract a custom type, you need to write a function to overload it. Write function in member function or global function
Function name operator + () {}
1. Overload of plus sign of member function
class person { public: person() {}; person(int a, int b) :m_A(a), m_B(b) {}; Plus operator overload, member function person operator+(person& p) { person temp; temp.m_A = this->m_A + p.m_A; temp.m_B = this->m_B + p.m_B; return temp; } int m_A; int m_B; }; void test01() { person p1(10, 20); person p2(20, 30); person p3 = p1 + p2; person p4 = p3.operator+(p2);//The two have the same function, that is, overloading the plus sign to the plus operator of class member functions cout << "p3 yes p1 and p2 After addition: m_A: " << p3.m_A << " m_B:" << p3.m_B << endl; cout << "p4 yes p4 and p2 After addition: m_A: " << p4.m_A << " m_B:" << p4.m_B << endl; }
There are two methods to implement function plus overloading in member functions
person p3 = p1 + p2; person p4 = p3.operator+(p2);//The two have the same function, that is, overloading the plus sign to the plus operator of class member functions
Add and subtract directly or use the functions in the class.
2. Overload of plus sign of global function
class person { public: person() {}; person(int a, int b) :m_A(a), m_B(b) {}; int m_A; int m_B; }; //Use global functions to overload the plus operator person operator+(person& p1, person& p2) { person temp; temp.m_A = p1.m_A + p2.m_A; temp.m_B = p1.m_B + p2.m_B; return temp; } void test01() { person p1(10, 20); person p2(20, 30); person p3 = p1 + p2; person p4 = operator+(p2, p3);//Plus overloading of global functions cout << "p3 yes p1 and p2 After addition: m_A: " << p3.m_A << " m_B:" << p3.m_B << endl; cout << "p4 yes p4 and p2 After addition: m_A: " << p4.m_A << " m_B:" << p4.m_B << endl; }
The plus sign overload of global functions can also use the plus sign + directly, or just like functions
person p3 = p1 + p2; person p4 = operator+(p2, p3);//Plus overloading of global functions
2. Shift left operator overload
Generally, we see ordinary shift left operators, which are often used in cout < < and can only identify the default data type of the compiler. If you want to output custom data types, you must overload the shift left operator.
Unlike the plus overloaded operator, the shift left overloaded operator cannot be written in member functions.
example:
class person { public: person() {}; person(int a, int b) { m_A = a; m_B = b; } int m_A ; int m_B; }; ostream& operator<<(ostream& cout, person& p) { cout << "m_A:" << p.m_A << " m_B:" << p.m_B; return cout; } void test01() { person p1(10,20); cout << p1 << endl; }
In the example, we can see that because cout belongs to ostream, which is similar to the data type, the call parameter type is ostream, and the return value is also ostream.
3. Overload of pre post increment operator
Like the previous two operators, the pre post increment operator is also an overload of custom operators.
Just like i + + or + + i, i -- or -- i.
Front++
First + +, then return the object.
class MyInteger { friend ostream& operator<<(ostream& cout, MyInteger& myint); public: MyInteger() { m_Num = 0; }; //Front overload MyInteger& operator++() { //Direct + + returns the object this->m_Num++; return *this; } private: int m_Num; }; ostream& operator<<(ostream& cout, MyInteger& myint) { cout << myint.m_Num; return cout; } void test01() { MyInteger myint; cout << ++myint << endl; }
The pre and post increment functions are all member functions, and because the custom data type is cout during output, there is also a left shift symbol overload.
Post++
Save a temporary value first, and then + +.
class MyInteger { friend ostream& operator<<(ostream& cout, MyInteger& myint); public: MyInteger() { m_Num = 0; }; //Post overload MyInteger& operator++(int) { //Save first with a temporary variable MyInteger temp = *this; m_Num++; return temp; } private: int m_Num; }; ostream& operator<<(ostream& cout, MyInteger& myint) { cout << myint.m_Num; return cout; } void test01() { MyInteger myint; cout << myint++ << endl; }
If the post function is overloaded, the value of the object needs to be saved first, and then + +.
When using pre or post, priority is given to the use of pre
Note here: when the return value is referenced, it returns an object, but when it is not referenced, it only returns a value.
4. Overload of pointer operator (smart pointer)
Smart pointer
It means that the pointer to the opened heap space can be released to avoid forgetting to release.
class person { public: person(int a) { this->m_A = a; } void showage() { cout << "Age is:" << this->m_A << endl; } private: int m_A; }; //Smart pointer, which is used to delete the opened heap space class smartpoint { public: smartpoint(person* person) { this->person = person; } ~smartpoint() { if (this->person != NULL) { delete this->person; this->person = NULL; } } private: person* person; }; void test01() { smartpoint(new person(20));//Open up to heap space, and classes can be automatically released }
The key points are:
smartpoint(person* person) { this->person = person; } ~smartpoint() { if (this->person != NULL) { delete this->person; this->person = NULL; } }
Free up heap space to avoid forgetting.
->Symbol overload
class smartpoint { public: person* operator->() { return this->person; } private: person* person; }; void test01() { smartpoint sp(new person(20));//Open up to heap space, and classes can be automatically released //After overloading, you can directly use the functions in person. sp->showage(); }
*Symbol overload
class smartpoint { public: person& operator*() { return *this->person; } private: person* person; }; void test01() { smartpoint sp(new person(20));//Open up to heap space, and classes can be automatically released (*sp).showage(); }
5. Overload of assignment operator
To avoid heap space duplication, it is also about deep copy and shallow copy.
class person2 { public: void operator=(const person2& p) { if (this->p_name != NULL) { delete[]this->p_name; this->p_name = NULL; } this->p_name = new char[strlen(p.p_name) + 1]; strcpy(this->p_name, p.p_name); } char* p_name; };