目錄
1.賦值運算符重載
1.1 運算符重載
1.2 賦值運算符重載
1.3 日期類實現?
1.賦值運算符重載
1.1 運算符重載
①當運算符被用于類類型的對象時,C++語言允許我們通過通過運算符重載的形式指定新的含義。C++規定類類型對象使用運算符時,必須轉換成調用對應運算符重載,若沒有對應的運算符重載,則會編譯報錯。
②運算符重載是具有特殊名字的函數,他的名字是由?operator?和后面要定義的運算符共同構成。和其他函數一樣,它也具有其返回類型和參數列表以及函數體。
③重載運算符函數的參數個數和該運算符作用的運算對象數量一樣多。一元運算符有一個參數,二元運算符有兩個參數,二元運算符的左側運算對象傳給第一個參數,右側運算對象傳給第二個參數。
④如果一個重載運算符函數是成員函數,則它的第一個運算對象默認傳給隱式的?this?指針,因此運算符重載作為成員函數時,參數比運算對象少一個。
⑤運算符重載以后,其優先級和結合性與對應的內置類型運算符保持一致。
⑥不能通過連接語法中沒有的符號來創建新的操作符:比如 operator@。
⑦.*? ::? sizeof? ?:? . 注意以上5個運算符不能重載。
⑧重載操作符至少有一個類類型參數,不能通過運算符重載改變內置類型對象的含義,如: int operator+(int x, int y)
⑨一個類需要重載哪些運算符,是看哪些運算符重載后有意義;重載++運算符時,有前置++和后置++,運算符重載函數名都是 operator++,無法很好的區分。C++規定,后置++重載時,增加一個 int 形參,跟前置++構成函數重載,方便區分。
⑩重載?<<?和?>>?時,需要重載為全局函數,因為重載為成員函數,this?指針默認搶占了第一個形參位置,第一個形參位置是左側運算對象,調用時就變成了 對象 <<cout,不符合使用習慣和可讀性。重載為全局函數把 ostream/istream?放到第一個形參位置就可以了,第二個形參位置當類類型對象。
Date?比較大小的代碼示例:
class Date {
public:Date(int year = 1, int month = 1, int day = 1) {_year = year;_month = month;_day = day;}//private:int _year;int _month;int _day;
};bool operator<(const Date& x1, const Date& x2)
{if (x1._year < x2._year){return true;}else if (x1._year == x2._year && x1._month < x2._month){return true;}else if (x1._year == x2._year&& x1._month == x2._month&& x1._day == x2._day){return true;}return false;
}int main()
{Date d1(2024, 8, 9);Date d2(2024, 8, 10);//bool ret2 = d1 < d2;bool ret2 = operator<(d1, d2);return 0;
}
1.2 賦值運算符重載
賦值運算符重載是一個默認成員函數,用于完成兩個已經存在的對象直接的拷貝賦值,
這里要注意跟拷貝構造區分,拷貝構造用于一個對象拷貝初始化給另一個要創建的對象。
如以下的例子:?
int main()
{Date d1(2024, 8, 10);Date d2(d1);Date d3(2024, 9, 11);Date d4 = d1;//拷貝構造d1 = d3; //賦值運算符重載return 0;
}
特點:
①賦值運算符重載是一個運算符重載,規定必須重載為成員函數。
賦值運算符重載的參數建議寫成 const 當前類類型引用,否則會傳值傳參會有拷貝。
②有返回值,且建議寫成當前類類型引用,引用返回可以提高效率,有返回值是為了支持連續賦值場景。?
③沒有顯式實現時,編譯器會自動生成一個默認賦值運算符重載,默認賦值運算符重載行為跟默認拷貝函數類似。(棧需要自己實現深拷貝)
④Date? 和? MyQueue 不需要進行賦值運算符重載,Stack 需要進行賦值運算符重載。
(①構造一般需要自己寫,自己傳參定義初始化;
???②析構,構造時有資源申請(如 malloc 或者 fopen)等,就需要顯示寫析構函數;
? ?③拷貝構造和賦值重載,顯示寫了析構,內部管理資源,就需要顯示實現拷貝構造;)
②的示例如下,這樣就支持了連續賦值。?
Date& operator = (const Date& d)
{_year = d._year;_month = d._month;_day = d._day;return *this;
}int main()
{d1 = d2 = d3;return 0;
}
?為了防止自己給自己賦值,會進行以下的代碼修改:
Date& operator = (const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}
?對于③,上述的代碼屏蔽之后,我們會發現,代碼依舊會正常運行。
1.3 日期類實現?
Date.h中的內容:
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;class Date
{
public:Date(int year = 1900, int month = 1, int day = 1);void Print();int GetMonthDay(int year, int month){assert(month > 0 && month < 13);static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };if (month ==2 &&(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 29;}return monthDayArray[month];}bool operator<(const Date& d);bool operator>(const Date& d);bool operator<=(const Date& d);bool operator>=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);// d1 += 天數Date& operator+=(int day);Date operator+(int day);// d1 -= 天數Date& operator-=(int day);Date operator-(int day);//++d1Date& operator++();//d1++Date operator++(int);//日期減去日期int operator-(const Date& d);
private:int _year;int _month;int _day;
};
?Date.cpp中的內容:
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;
}void Date::Print()
{cout << _year << "-" << _month << "-" << "_day" << endl;
}bool Date::operator<(const Date& d)
{if (_year < d._year){return true;}else if (_year == d._year && _month < d._month){return true;}else if (_year == d._year && _month == d._month && _day < d._day){return true;}return false;
}
bool Date::operator>(const Date& d)
{return !(*this <= d);
}
bool Date::operator<=(const Date& d)
{return *this < d || *this == d;
}
bool Date::operator>=(const Date& d)
{return !(*this < d);
}
bool Date::operator==(const Date& d)
{return _year = d._year &&_month == d._month &&_day == d._day;
}
bool Date::operator!=(const Date& d)
{return !(*this == d);
}
//這種不推薦,推薦下面的方法
//Date Date::operator+(int day)
//{
// Date tmp = *this;
// tmp._day += day;
// while (tmp._day > GetMonthDay(tmp._year, tmp._month))
// {
// tmp._day -= GetMonthDay(tmp._year, tmp._month);
// ++tmp._month;
// if (tmp._month == 13)
// {
// tmp._year++;
// tmp._month = 1;
// }
// }
//
// return tmp;
//}
//
//Date& Date::operator+=(int day)
//{
// *this = *this + day;
// return *this;
//}Date& Date::operator+=(int day)
{if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){++_month;if (_month == 13){_year++;_month = 1;}}return *this;
}Date Date::operator+(int day)
{Date tmp = *this;tmp += day;return tmp;
}
Date& Date::operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}
Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}//++d1
Date& Date::operator++()
{*this += 1;return *this;
}
//d1++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;//后置返回++以前的值
}int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag;
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??