Overload of assignment operator
Operator overloading
In order to enhance the readability of the code, C + + introduces operator overloading. Operator overloading is a function with special function name, and also has its return value type, function name and parameter list. Its return value type and parameter list are similar to ordinary functions.
The function name is: the keyword operator is followed by the operator symbol that needs to be overloaded.
Function prototype: return value type operator operator (parameter list)
be careful:
You cannot create a new operator by concatenating other symbols: for example, operator@
Overloaded operators must have an operand of class type or enumeration type
The meaning of operators used for built-in types cannot be changed. For example, the meaning of built-in integer +
When an overloaded function is a class member, its formal parameters appear to be 1 less than the number of operands. The operator of the member function has a default formal parameter this, which is limited to the first formal parameter
.* ,:: ,sizeof ,?: ,. Note that the above five operators cannot be overloaded. This often appears in written multiple-choice questions.
// Global operator== class Date { public: Date(int year = 1900, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } //private: int _year; int _month; int _day; }; // It will be found here that if the operator is overloaded to be global, the member variables need to be shared. Then the problem comes. How to ensure the encapsulation? // In fact, this can be solved with the friends we will learn later, or simply overloaded into member functions. bool operator==(const Date& d1, const Date& d2) { return d1._year == d2._year; && d1._month == d2._month && d1._day == d2._day; } void Test () { Date d1(2018, 9, 26); Date d2(2018, 9, 27); cout<<(d1 == d2)<<endl; }
class Date { public: Date(int year = 1900, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } // bool operator==(Date* this, const Date& d2) // It should be noted here that the left operand is the object of the calling function pointed to by this bool operator==(const Date& d2) { return _year == d2._year; && _month == d2._month && _day == d2._day; } private: int _year; int _month; int _day; }; void Test () { Date d1(2018, 9, 26); Date d2(2018, 9, 27); cout<<(d1 == d2)<<endl; }
Assignment operator overload
class Date { public : Date(int year = 1900, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } Date (const Date& d) { _year = d._year; _month = d._month; _day = d._day; } Date& operator=(const Date& d) { if(this != &d) { _year = d._year; _month = d._month; _day = d._day; } } private: int _year ; int _month ; int _day ; };
Assignment operators mainly have four points:
-
Parameter type
2. Return value
3. Check whether you assign values to yourself
4. Return to * this
- If a class does not explicitly define an assignment operator overload, the compiler will also generate one to complete the byte order value copy of the object.
class Date { public: Date(int year = 1900, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } private: int _year; int _month; int _day; }; int main() { Date d1; Date d2(2018,10, 1); // Here, the compiler called by d1 generates operator = to complete the copy, and the values of d2 and d1 are the same. d1 = d2; return 0; }
Then the default assignment overloaded function generated by the compiler can copy the value of byte order. Do we still need to implement it ourselves? Of course, classes such as date classes are not necessary. What about the following classes? Try to verify it?
// Here you will find that the following program will crash? This needs to be solved by the deep copy we will talk about later. class String { public: String(const char* str = "") { _str = (char*)malloc(strlen(str) + 1); strcpy(_str, str); } ~String() { cout << "~String()" << endl; free(_str); } private: char* _str; }; int main() { String s1("hello"); String s2("world"); s1 = s2; }
Implementation of date class
class Date { public: // Gets the number of days in a month of a year int GetMonthDay(int year, int month) { static int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int day = days[month]; if (month == 2 &&((year % 4 == 0 && year % 100 != 0) || (year%400 == 0))) { day += 1; } return day; } // Full default constructor Date(int year = 1900, int month = 1, int day = 1); // copy constructor // d2(d1) Date(const Date& d); // Assignment operator overload // d2 = d3 -> d2.operator=(&d2, d3) Date& operator=(const Date& d); // Destructor ~Date(); // Date + = days Date& operator+=(int day); // Date + days Date operator+(int day); // Date - Days Date operator-(int day); // Date - = days Date& operator-=(int day); // Front++ Date& operator++(); // Post++ Date operator++(int); // Post-- Date operator--(int); // Front-- Date& operator--(); // >Operator overloading bool operator>(const Date& d); // ==Operator overloading bool operator==(const Date& d); // >=Operator overloading inline bool operator >= (const Date& d); // < operator overload bool operator < (const Date& d); // < = operator overload bool operator <= (const Date& d); // != Operator overloading bool operator != (const Date& d); // Date - date return days int operator-(const Date& d); private: int _year; int _month; int _day; };