網(wǎng)站下要加個備案號 怎么做上海推廣系統(tǒng)
目錄
1.賦值運算符重載
1.1運算符重載
1.2賦值運算符重載
1.2.1賦值運算符重載格式
1.2.2賦值運算符只能重載成成員函數(shù)不能重載成全局函數(shù)?
1.2.3同拷貝函數(shù)一樣,如果類是形如日期類這樣變量全是內(nèi)置類型的,賦值運算符就必須自己實現(xiàn),用編譯器默認(rèn)生成的就行,但是如果涉及到資源管理就必須要自己實現(xiàn)賦值運算符。
1.3前置++和后置++重載
2.const成員函數(shù)
3.取地址及const取地址操作符重載
?
????????????????????????????????????????????????????????????????時光不語,我們都在努力
這里是來自M--Y的專欄:C++啟航
以下內(nèi)容均為個人見解,如有不足還請指出
期待大家的點贊、收藏、評論(互三必回)諸君共勉
類中的6個默認(rèn)成員函數(shù)
1.賦值運算符重載
1.1運算符重載
?????????運算符重載提高代碼可讀性。函數(shù)命名為關(guān)鍵字operator后面接需要重載的運算符符號
特別注意:
????????1.不能通過連接其他符號來創(chuàng)建新的操作符,如operator@
? ? ? ? 2.重載操作符必須有一個類類型參數(shù)
? ? ? ? 3.用于內(nèi)置類型的運算符,其含義不能改變
? ? ? ? 4.作為類成員函數(shù)重載時,其形參看起來比操作數(shù)數(shù)目少1,這是因為成員函數(shù)的第一個參數(shù)是隱式的this
? ? ? ? 5..*? ? ? ? ::? ? ? ? sizeof? ? ? ? ?;? ? ? ? .這五個運算符不支持重載
class Date
{
public:
? ? ? ?// ……
private:
? ? ? ? int? _year;
? ? ? ? int? _month;
? ? ? ? int? _day;
}?
bool??operator==(const? Date& d1,const? Date& d2)
{
? ? ? ? //……
}
如果和上述代碼一樣定義成全局函數(shù)的話,就無法訪問到Date類中的私有成員變量,如果要使代碼編譯通過的話,就必須將上述代碼標(biāo)紅處給注釋掉。
這樣處理的話,顯得過于麻煩,所以不如將函數(shù)封裝成成員函數(shù)。
封裝成成員函數(shù)的時候我們需要注意一點,上面注意4提到,成員函數(shù)的第一個參數(shù)是隱式的this所以在代碼上要做些許調(diào)整。
class Date
{
public:
? ? ? ? bool operator==(const Date& d)
? ? ? ? {
? ? ? ? ? ? ? ? return _year==d._year
? ? ? ? ? ? ? ? &&_month=d._month
? ? ? ? ? ? ? ? &&_day==d._month;
????????}
private:
? ? ? ? int _year;
? ? ? ? int _month;
? ? ? ? int _day;
} ;
1.2賦值運算符重載
1.2.1賦值運算符重載格式
? ? ? ? 1.參數(shù)類型:const T&(傳遞引用可以提高傳參效率)
? ? ? ? 2.返回值類型:T&。返回引用可以提高效率,同時在有些情況下可以支持連續(xù)賦值
? ? ? ? 3.檢測是否自己給自己賦值
? ? ? ? 4.返回*this。目的:連續(xù)賦值,下文會細講
1.2.2賦值運算符只能重載成成員函數(shù)不能重載成全局函數(shù)?
原因:如果用戶將賦值運算符實現(xiàn)成全局的話,編譯器就會自己生成一個默認(rèn)的成員函數(shù),這樣兩者就會產(chǎn)生沖突了
class Date
{
public:
? ? ? ? //……
? ? ? ? Date& operator=(const Date&d)
? ? ? ? {
? ? ? ? ? ? ? ? _year=d._year;
? ? ? ? ? ? ? ? _month=d._month;
? ? ? ? ? ? ? ? _day=d._day;?
????????????????
? ? ? ? ? ? ? ? return *this;
? ? ? ? }
};
1.2.3同拷貝函數(shù)一樣,如果類是形如日期類這樣變量全是內(nèi)置類型的,賦值運算符就必須自己實現(xiàn),用編譯器默認(rèn)生成的就行,但是如果涉及到資源管理就必須要自己實現(xiàn)賦值運算符。
1.3前置++和后置++重載
思考:由上面敘述可以聯(lián)想到關(guān)于++的運算符重載的函數(shù)名應(yīng)該是operator++,但是這樣的話如何區(qū)分前置++和后置++呢?
為了區(qū)分,C++特別規(guī)定:后置++重載時多加一個int類型的參數(shù),但是調(diào)用時不用傳參,編譯器會自動傳遞。
//前置++
Date& operator++()?
{
? ? ? ? _day++;
? ? ? ? _month++;
? ? ? ? _year++;
? ? ? ? if(_day>GetMonthday())//實現(xiàn)輸出某年某月的天數(shù)
? ? ? ? {
? ? ? ? ? ? ? ? _day-=GetMonthday();
? ? ? ? ? ? ? ? _month++
????????}
? ? ? ? if(_month>=13)
? ? ? ? {
? ? ? ? ? ? ? ? _month-=12;
? ? ? ? ? ? ? ? _year++;
????????}
? ? ? ? return *this;
}
后置++
//注意后置++是先是有后+1,因此需要一個臨時變量來保存原來的值,所以函數(shù)的返回值應(yīng)該是Date而不是Date&
Date operator++(int)
{
? ? ? ? Date temp=*this;
? ? ? ? //重復(fù)上述前置++步驟
? ? ? ? return temp;
}?
?其實關(guān)于運算符的重載有很多+=,-=,+,-,>,<,>=,<=。但是如果每個運算符都按上述方式代碼就會顯得十分冗長。有沒有什么辦法呢?? ? ? ? 答案是肯定的。
Date& Date:: operator+=(int day)//日期+天數(shù)=日期
{
?? ?_day += day;?? ?while (_day > GetMonthDay(_year, _month))
?? ?{
?? ??? ?_day -= GetMonthDay(_year, _month);
?? ??? ?_month++;
?? ??? ?if (_month == 13)
?? ??? ?{
?? ??? ??? ?_year++;
?? ??? ??? ?_month = 1;
?? ??? ?}
?? ?}?? ?while (_day < 0)
?? ?{
?? ??? ?_month--;
?? ??? ?if (_month == 0)
?? ??? ?{
?? ??? ??? ?_year--;
?? ??? ??? ?_month = 12;
?? ??? ?}
?? ??? ?_day += GetMonthDay(_year, _month);
?? ?}?? ?return *this;
}
以上上+=為例,當(dāng)我們實現(xiàn)一個運算符重載后,和它相關(guān)的運算符重載就會變得很簡單實現(xiàn),只需要進行復(fù)用就行?。
Date& operator-=(int day)//日期-天數(shù)=日期
{
? ? ? ? return *this+=-day
}
?Date& operator++()
{
? ? ? return *this+=1;
}
Date operator++(int)
{
? ? ? ? Date temp=*this;
? ? ? ? *this+=1;
? ? ? ? return temp;
}?
?其他的運算符重載也和這類似,感興趣的可以自己去嘗試實現(xiàn)。
2.const成員函數(shù)
用cosnt修飾的成員函數(shù)被稱為const函數(shù),實際上const修飾的是成員函數(shù)中的this,但是一般const修飾變量會加在其前面,而this是隱式的,如何表示修飾的是this呢?
C++規(guī)定將cosnt+在函數(shù)后面,就表示對this的修飾。
Date operator+(int day) const
{
? ? ? ? //……
}
3.取地址及const取地址操作符重載
這兩個函數(shù)一般不用重新定義,不是特殊情況,編譯器會自動生成的夠用。
Date* operator&()
{
? ? ? ? return this;
}
const Date* operator&() const
{
? ? ? ? return this;
}
?