npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] dev: `webpack-dev-server` n
阿新 • • 發佈:2021-02-10
技術標籤:C++面向物件
友元的目的就是讓一個函式或者類訪問另一個類中私有成員
友元的關鍵字 friend
友元的三種實現
全域性函式做友元
類做友元
成員函式做友元
1.全域性函式做友元
當然也可以寫成引用的形式
全域性函式是不能訪問類中的私有成員的,這時要想其能訪問就要在類中將這個函式宣告為友元函式
friend void goodGay(Building *building);
``
```cpp
#include<iostream>
#include<string>
using namespace std;
class Building {
//告訴編譯器goofGay全域性函式是Building類的好朋友,可以訪問類中的私有內容
friend void goodGay(Building *building);
friend void goodGay(Building &building);
public:
string m_sittingroom;//客廳
Building(){
m_sittingroom = "客廳";
m_bedroom = "臥室";
}
private:
string m_bedroom;//臥室
};
//全域性函式
void goodGay(Building *building){
cout << "好基友正在訪問:" << building->m_sittingroom << endl;
cout << "好基友正在訪問:" << building->m_bedroom << endl;
}
void goodGay(Building &building) {//引用形式
cout << "好基友正在訪問:" << building.m_sittingroom << endl;
cout << "好基友正在訪問:" << building.m_bedroom << endl;
}
void test01() {
Building building;
goodGay(building);
goodGay(&building);
}
/*void test02() {
const Person p;//常物件
p.m_A = 100;//常物件下成員變數不可以修改
p.m_B = 100;//加了關鍵字mutable在常物件下也可以修改
//常物件只能呼叫常函式,不可以呼叫普通成員函式,因為普通成員函式可以修改屬性
p.showPerson();
p.dunc();
}*/
int main() {
test01();
return 0;
}
2.友元類
當一個類要訪問另一個類中的私有成員時,要在要訪問的類中先宣告自己是友元類,就可以訪問該類的私有成員函數了
#include<iostream>
#include<string>
using namespace std;
class Building {
//GoodGay這個類是Building類的好朋友可以訪問這個類的私有成員
friend class GoodGay;
public:
string m_sittingroom;//客廳
Building();
private:
string m_bedroom;//臥室
};
//類外寫成員函式 函式返回型別 類名::類內函式
Building::Building() {
m_sittingroom = "客廳";
m_bedroom = "臥室";
}
class GoodGay {
public:
GoodGay();
void visit();//參觀函式,訪問Building中的屬性
Building* building;
};
GoodGay::GoodGay() {
//建立建築物物件
building = new Building;
}
void GoodGay :: visit() {
cout << "好基友正在訪問:" << building->m_sittingroom << endl;
cout << "好基友正在訪問:" << building->m_bedroom << endl;
}
void test01() {
GoodGay gg;
gg.visit();
}
int main() {
test01();
return 0;
}
3.成員函式做友元
在要訪問的類中宣告
friend 函式返回型別 類名::成員函式名();
在此處要注意一個問題,即兩個類的程式碼位置,如果呼叫是類中的成員函式做友元的話,應該將該類的整個定義放在前面,否則編譯器識別不到,依然報錯,不可訪問
因為這個裡面涉及到了類的成員函式,如果你只是簡單的聲明瞭一下類,那編譯器只能識別類而不能識別裡面的函式。
#include<iostream>
#include<string>
using namespace std;
class Building;
class GoodGay {
public:
GoodGay();
void visit();//參觀函式,訪問Building中的屬性
void visit2();//讓該函式不可以訪問Building中的私有成員
Building* building;
};
class Building {
//告訴編譯器GoodGay類下的vist()函式是該類的友元函式,可以訪問該類的私有成員
friend void GoodGay::visit();
public:
string m_sittingroom;//客廳
Building();
private:
string m_bedroom;//臥室
};
//類外寫成員函式 函式返回型別 類名::類內函式
Building::Building() {
m_sittingroom = "客廳";
m_bedroom = "臥室";
}
GoodGay::GoodGay() {
//建立建築物物件
building = new Building;
}
void GoodGay :: visit() {
cout << "好基友正在訪問:" << building->m_sittingroom << endl;
cout << "好基友正在訪問:" << building->m_bedroom << endl;
}
void GoodGay::visit2() {
cout << "好基友正在訪問:" << building->m_sittingroom << endl;
//cout << "好基友正在訪問:" << building->m_bedroom << endl;
}
void test01() {
GoodGay gg;
gg.visit();
gg.visit2();
}
int main() {
test01();
return 0;
}