文章内容转载自黑马程序员C++核心编程讲义,如有侵权,请联系作者删除
4.4 友元
生活中你的家有客厅(Public),有你的卧室(Private)
客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去
但是呢,你也可以允许你的好闺蜜好基友进去。
在程序里,有些私有属性
也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术
友元的目的就是让一个函数或者类 访问另一个类中私有成员
友元的关键字为 ==friend==
友元的三种实现
4.4.1 全局函数做友元
class Building { friend void goodGay(Building * building);
public:
Building() { this->m_SittingRoom = "客厅"; this->m_BedRoom = "卧室"; }
public: string m_SittingRoom;
private: string m_BedRoom; };
void goodGay(Building * building) { cout << "好基友正在访问: " << building->m_SittingRoom << endl; cout << "好基友正在访问: " << building->m_BedRoom << endl; }
void test01() { Building b; goodGay(&b); }
int main(){
test01();
system("pause"); return 0; }
|
自己写的:
#include<iostream> using namespace std; class Building { friend void goodGay(Building* building); public: Building(){ m_livingroom = "客厅"; m_bedroom = "卧室"; } public: string m_livingroom; private: string m_bedroom;
};
void goodGay(Building *building) { cout << "全局函数正在访问:" << building->m_livingroom << endl;
cout << "全局函数正在访问:" << building->m_bedroom << endl; } void test01() { Building building; goodGay(&building); }
int main() { test01(); return 0; }
|
4.4.2 类做友元
class Building; class goodGay { public:
goodGay(); void visit();
private: Building *building; };
class Building { friend class goodGay;
public: Building();
public: string m_SittingRoom; private: string m_BedRoom; };
Building::Building() { this->m_SittingRoom = "客厅"; this->m_BedRoom = "卧室"; }
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();
system("pause"); return 0; }
|
自己写的:
#include<iostream> using namespace std; class Building; class GoodGay { public: GoodGay(); void visit();
Building* building; }; class Building { friend class GoodGay; public: Building(); public: string m_livingRoom;
private: string m_bedRoom; };
Building::Building() { m_livingRoom = "客厅"; m_bedRoom = "卧室"; }
GoodGay::GoodGay() { building = new Building; }
void GoodGay::visit() { cout << "好基友正在访问" << building->m_livingRoom << endl;
cout << "好基友正在访问" << building->m_bedRoom << endl; } void test01() { GoodGay ss; ss.visit(); } int main() { test01(); return 0; }
|
4.4.3 成员函数做友元
class Building; class goodGay { public:
goodGay(); void visit(); void visit2();
private: Building *building; };
class Building { friend void goodGay::visit();
public: Building();
public: string m_SittingRoom; private: string m_BedRoom; };
Building::Building() { this->m_SittingRoom = "客厅"; this->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; }
void test01() { goodGay gg; gg.visit();
}
int main(){ test01();
system("pause"); return 0; }
|
自己写的:
#include<iostream> using namespace std; class Building; class GoodGay { public: GoodGay(); void visit();
void visit2();
Building* building; }; class Building { friend void GoodGay::visit(); public: Building(); public: string m_livingRoom; private: string m_bedRoom; };
Building::Building() { m_livingRoom = "客厅"; m_bedRoom = "卧室"; }
GoodGay::GoodGay() { building = new Building; }
void GoodGay::visit() { cout << "visit函数正在访问" << building->m_livingRoom << endl; cout << "visit函数正在访问" << building->m_bedRoom << endl; }
void GoodGay::visit2() { cout << "visit函数正在访问" << building->m_livingRoom << endl; }
void test01() { GoodGay ss; ss.visit(); }
void test02() { GoodGay ss; ss.visit2(); }
int main() { test01(); test02(); return 0; }
|