日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C++示例講解friend?static?const關(guān)鍵字的用法_C 語(yǔ)言

作者:努力變好的zz ? 更新時(shí)間: 2022-08-10 編程語(yǔ)言

一、友元函數(shù)

1.1重載operator<<

問(wèn)題:現(xiàn)在我們嘗試去重載operator<<,然后發(fā)現(xiàn)我們沒(méi)辦法將operator<<重載成成員函數(shù)。因?yàn)閏out的輸出流對(duì)象和隱含的this指針在搶占第一個(gè)參數(shù)的位置。this指針默認(rèn)是第一個(gè)參數(shù)也就是左操作數(shù)了。但是實(shí)際使用中cout需要是第一個(gè)形參對(duì)象,才能正常使用。所以我們要將operator<<重載成全局函數(shù)。但是這樣的話,又會(huì)導(dǎo)致類外沒(méi)辦法訪問(wèn)成員,那么這里就需要友元來(lái)解決。operator>>同理。

class Date
{
public:
Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{}
ostream& operator<<(ostream& _cout)
{
_cout<<d._year<<"-"<<d._month<<"-"<<d._day;
return _cout;
}
prvate:
int _year;
int _month;
int _day
};
int main()
{
Date d(2017, 12, 24);
d<<cout;
return 0;
}

但"d<<cout"可讀性很低,所以我們嘗試需要在類外部重載operator<<

1.2友元函數(shù)

友元函數(shù)可以直接訪問(wèn)類的私有成員,它是定義在類外部的普通函數(shù),不屬于任何類,但需要在類的內(nèi)部聲明,聲明時(shí)需要加friend關(guān)鍵字。

class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout<<d._year<<"-"<<d._month<<"-"<<d._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
_cin>>d._year;
_cin>>d._month;
_cin>>d._day;
return _cin;
}
int main()
{
Date d;
cin>>d;
cout<<d<<endl;
return 0;

1.友元函數(shù)可訪問(wèn)類的私有和保護(hù)成員,但不是類的成員函數(shù)

2.友元函數(shù)不能用const修飾

3.友元函數(shù)可以在類定義的任何地方聲明,不受類訪問(wèn)限定符限制

4.一個(gè)函數(shù)可以是多個(gè)類的友元函數(shù)

5.友元函數(shù)的調(diào)用與普通函數(shù)的調(diào)用和原理相同

1.3友元類

1.友元類的所有成員函數(shù)都可以是另一個(gè)類的友元函數(shù),都可以訪問(wèn)另一個(gè)類中的非公有成員。

2.友元關(guān)系是單向的,不具有交換性。

比如上述Time類和Date類,在Time類中聲明Date類為其友元類,那么可以在Date類中直接訪問(wèn)Time類的私有成員變量,但想在Time類中訪問(wèn)Date類中私有的成員變量則不行。

3.友元關(guān)系不能傳遞如果B是A的友元,C是B的友元,則不能說(shuō)明C時(shí)A的友元。

class Date; // 前置聲明
class Time
{
friend class Date; // 聲明日期類為時(shí)間類的友元類,則在日期類中就直接訪問(wèn)Time類中的私有成
員變量
public:
Time(int hour, int minute, int second)
: _hour(hour)
, _minute(minute)
, _second(second)
{}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
void SetTimeOfDate(int hour, int minute, int second)
{
// 直接訪問(wèn)時(shí)間類私有的成員變量
_t._hour = hour;
_t._minute = minute;
_t.second = second;
}
private:
int _year;
int _month;
int _day;
Time _t;
};

二、關(guān)鍵字const

2.1const修飾類的成員函數(shù)

將const修飾的類成員函數(shù)稱之為const成員函數(shù),const修飾類成員函數(shù),實(shí)際修飾該成員函數(shù)隱含的this指針,表明在該成員函數(shù)中不能對(duì)類的任何成員進(jìn)行修改。

總結(jié):

1.const對(duì)象不能調(diào)用非cosnt成員函數(shù)(權(quán)限放大:const Date* -> Date*)

2.非const對(duì)象可以調(diào)用const成員函數(shù)

3.const成員函數(shù)不能調(diào)用非cosnt成員函數(shù)(權(quán)限放大)

4.非cosnt成員函數(shù)可以調(diào)用const成員函數(shù)

三、關(guān)鍵字static

3.1static類成員

概念:聲明為static的類成員稱為類的靜態(tài)成員,用static修飾的成員變量,稱之為靜態(tài)成員變量;用static修飾的成員函數(shù),稱之為靜態(tài)成員函數(shù)。靜態(tài)的成員變量一定要在類外進(jìn)行初始化

特性:

  • 靜態(tài)成員為所有類對(duì)象所共享,不屬于某個(gè)具體的實(shí)例
  • 靜態(tài)成員變量必須在類外定義,定義時(shí)不添加static關(guān)鍵字
  • 類靜態(tài)成員即可用類名::靜態(tài)成員或者對(duì)象.靜態(tài)成員來(lái)訪問(wèn)
  • 靜態(tài)成員函數(shù)沒(méi)有隱藏的this指針,不能訪問(wèn)任何非靜態(tài)成員
  • 靜態(tài)成員和類的普通成員一樣,也有public、protected、private3種訪問(wèn)級(jí)別,也可以具有返回值

3.2面試題

面試題:實(shí)現(xiàn)一個(gè)類,計(jì)算中程序中創(chuàng)建出了多少個(gè)類對(duì)象。

class A
{
public:
A() {++_scount;}
A(const A& t) {++_scount;}
static int GetACount() { return _scount;}
private:
static int _scount;
};
int A::_count = 0;
void TestA()
{
cout<<A::GetACount()<<endl;
A a1, a2;
A a3(a1);
cout<<A::GetACount()<<endl;
}

注意:

1.靜態(tài)成員函數(shù)不能調(diào)用非靜態(tài)成員函數(shù)

2.非靜態(tài)成員函數(shù)可以調(diào)用靜態(tài)成員函數(shù)

總結(jié)

以上就是關(guān)于C++關(guān)鍵字friend ,static,const 全部總結(jié),感謝大家的閱讀觀看

原文鏈接:https://blog.csdn.net/bitezz/article/details/125238160

欄目分類
最近更新