客厅就是Public,你的卧室就是Private
客厅所有人都可以进去,但是你的卧室只有和你亲密的人可以进。
在程序中,有些私有属性也想让类外特殊的一些函数或者类进行访问,就需要用到友元技术。
友元的目的就是让一个函数或者类 访问另一个类中的私有元素。
友元的关键字——friend
友元的三种实现
就是将此函数在类的最上面写一个声明,前面加一个friend。
- #include
- #include
- using namespace std;
- class Building
- {
- //goodgay全局函数是Building类的一个好朋友,可以访问你家的卧室(私有成员)
- friend void goodgay(Building* building);
- public:
- Building()
- {
- m_SittingRoom = "客厅";
- 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 test()
- {
- Building building;
- goodgay(&building);
- }
- int main(void)
- {
- test();
- system("pause");
- return 0;
- }
-
一个类在另一个中friend class xx。
- #include
- #include
- using namespace std;
- //在前面先声明一下
- class Building;
-
- class GoodGay
- {
- public:
- GoodGay();
- public:
- void visit();//参观函数 访问Building中的属性(公共属性和私有)
- Building* building;
- };
-
-
- class Building
- {
- //GoodGay是Building类的好朋友,可以访问其私有属性
- friend class GoodGay;
- public:
- Building();
- public:
- string m_SittingRoom;
- private:
- string m_BedRoom;
- };
- //在类外写成员函数
- Building::Building()
- {
- m_SittingRoom = "客厅";
- m_BedRoom = "卧室";
- }
- GoodGay::GoodGay()
- {
- //创建一个Building对象
- building = new Building;
- }
- void GoodGay::visit()
- {
- cout << "好基友正在访问你的" << building->m_SittingRoom << endl;
- cout << "好基友正在访问你的" << building->m_BedRoom << endl;
- }
-
- void test()
- {
- GoodGay gy;
- gy.visit();
- }
- int main(void)
- {
- test();
- system("pause");
- return 0;
- }
- //在你的代码中,building = new Building; 是在 GoodGay 类的构造函数 GoodGay::GoodGay() 中创建了一个 Building 类的对象,然后将其地址存储在 GoodGay 类的 building 成员变量中。
-
- 具体解释如下:
-
- GoodGay::GoodGay() 构造函数:
-
- 这是 GoodGay 类的构造函数,它在 GoodGay 类的对象被创建时调用。
- 在构造函数内部,building = new Building; 执行了动态内存分配操作,创建了一个新的 Building 类的对象,并返回该对象的地址。
- 这个地址被存储在 GoodGay 类的 building 成员变量中,因此 building 成员变量现在指向了一个新的 Building 对象。
- Building::Building() 构造函数:
-
- 这是 Building 类的构造函数,用于初始化 Building 类的对象。
- 在构造函数内部,m_SittingRoom 和 m_BedRoom 成员变量都被初始化为特定的字符串值。
- 所以,building = new Building; 的目的是在 GoodGay 对象被创建时,同时创建了一个关联的 Building 对象,以便 GoodGay 对象可以访问 Building 对象的成员变量,包括 m_SittingRoom 和 m_BedRoom。这种关系通常用于表示两个类之间的友好关系,允许一个类的成员函数访问另一个类的私有成员。在你的代码中,GoodGay 类被声明为 Building 类的好朋友(friend class GoodGay;),因此它可以访问 Building 类的私有成员。
告诉编译器 另一个类中的xx成员函数作为本类的好朋友,可以访问私有函数。
- #include
- #include
- using namespace std;
-
- class Building;
- class GoodGay
- {
- public:
- GoodGay();
- void visit();//可以访问Building中私有成员
- void visit1();//不可以访问Building中私有成员
- Building* builidng;
- };
- class Building
- {
- //告诉编译器 GoodGay类中的visit成员函数作为本类的好朋友,可以访问私有函数
- friend void GoodGay::visit();
- public:
- Building();
- public:
- string m_SittingRoom;
- private:
- string m_BedRoom;
- };
-
- Building::Building()
- {
- m_SittingRoom = "客厅";
- m_BedRoom = "卧室";
- }
-
- GoodGay::GoodGay()
- {
-
- builidng = new Building;
- }
- void GoodGay::visit()
- {
- cout << "visit正在访问" << builidng->m_SittingRoom << endl;
- cout << "visit正在访问" << builidng->m_BedRoom << endl;
- }
- void GoodGay::visit1()
- {
- cout << "visit1正在访问" << builidng->m_SittingRoom << endl;
-
- }
- void test()
- {
- GoodGay gg;
- gg.visit();
- gg.visit1();
- }
- int main(void)
- {
- test();
- system("pause");
- return 0;
- }
-