Lesson 23 Operator Overloading
Operator overloading
Overloaded assignment operators
person& person::operator= (const person& Other) {//Check self-assignment if (this = = &other) {return *this;} Release the original memory resource delete[]m_data;int length = strlen (other.m_data); m_data = new Char[length + 1];strcpy (m_data, Other.m_data) ;//Return the reference to this object return *this;}
Lesson 25 self-increment operator overloading
Operator overloading
Difference between pre-increment and post-increment
Incremental modification of an object (operand) when using the pre-increment, and then returning the object
When using post-increment, you must return the original object value before the increment
Class increase{public:increase (int x): value (x) {}increase& operator + + ();//pre-increment increase operator + + (int);// After increment private:int value;};
Overloads of the member form of + +
increase& Increase::operator + + ()//pre-increment {value++;//First increment return *this;//return an object}increase Increase::operator + + (int)// After increment {increase temp (*this);//Temporary object holds original object value value++;//original object increment change return temp;//return original object value}
NetEase Cloud Classroom _c++ Development Introduction to Mastery _ Chapter 4: operator overloading