- 新增现代 C++ 教程的 Preface 章节,包括英文和中文版本 - 添加 C++ Primer 练习代码 - 新增 Learn C++ 教程的 C++ 开发简介章节 - 添加头文件解析文档 - 更新 mkdocs.yml,包含新教程的目录结构 - 修改项目设置,使用 Python 3.10环境
239 lines
4.4 KiB
Markdown
239 lines
4.4 KiB
Markdown
# C++的友元
|
||
|
||
# 1. 友元简介
|
||
|
||
① 生活中你的家有客厅(Public),有你的卧室(Private),客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去,但是呢,你也可以允许你的好闺蜜进去。
|
||
|
||
② 在程序里,有些私有属性,也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术。
|
||
|
||
③ 友元的目的就是让一个函数或者类访问另一个类中私有成员。
|
||
|
||
④ 友元的关键字为 friend。
|
||
|
||
⑤ 友元的三种实现:
|
||
|
||
1. 全局函数做友元。
|
||
2. 类做友元。
|
||
3. 成员函数做友元。
|
||
|
||
|
||
```python
|
||
#include <iostream>
|
||
using namespace std;
|
||
|
||
//建筑物类
|
||
class Buiding
|
||
{
|
||
//goodfriend全局函数是Buiding类好朋友,可以访问Buiding中私有成员
|
||
friend void goodfriend(Buiding* buiding);
|
||
|
||
public:
|
||
Buiding() //构造函数 赋初值
|
||
{
|
||
m_SittingRoom = "客厅";
|
||
m_BedRoom = "卧室";
|
||
}
|
||
|
||
public:
|
||
string m_SittingRoom; //客厅
|
||
|
||
private:
|
||
string m_BedRoom;
|
||
};
|
||
|
||
//全局函数
|
||
void goodfriend(Buiding *buiding)
|
||
{
|
||
cout << "好基友全局函数 正在访问:" << buiding->m_SittingRoom << endl;
|
||
cout << "好基友全局函数 正在访问:" << buiding->m_BedRoom << endl;
|
||
|
||
}
|
||
|
||
void test01()
|
||
{
|
||
Buiding building;
|
||
goodfriend(&building);
|
||
}
|
||
|
||
int main()
|
||
{
|
||
test01();
|
||
|
||
system("pause");
|
||
|
||
return 0;
|
||
|
||
}
|
||
```
|
||
|
||
运行结果:
|
||
- 好基友全局函数 正在访问:客厅
|
||
- 好基友全局函数 正在访问:卧室
|
||
- 请按任意键继续. . .
|
||
|
||
# 2. 友元类
|
||
|
||
|
||
```python
|
||
#include <iostream>
|
||
using namespace std;
|
||
|
||
//类做友元
|
||
class Building; //声明
|
||
|
||
class GoodGay
|
||
{
|
||
public:
|
||
GoodGay();
|
||
|
||
void visit(); //参观函数 访问Building中的属性
|
||
|
||
Building* building;
|
||
};
|
||
|
||
class Building
|
||
{
|
||
//GoodGay类是本类的好朋友,可以访问本类的私有成员
|
||
friend class GoodGay;
|
||
|
||
public:
|
||
Building();
|
||
|
||
public:
|
||
string m_SittingRoom; //客厅
|
||
private:
|
||
string m_BedRoom; //卧室
|
||
};
|
||
|
||
//类外写成员函数,写类名的作用域
|
||
|
||
//Building构造函数
|
||
Building::Building()
|
||
{
|
||
m_SittingRoom = "客厅";
|
||
m_BedRoom = "卧室";
|
||
}
|
||
|
||
//GoodGay构造函数
|
||
GoodGay::GoodGay()
|
||
{
|
||
//创建建筑物对象
|
||
building = new Building; //这里会调用Building的构造函数
|
||
//new是创建什么样的数据类型,就返回什么样的数据类型的指针
|
||
}
|
||
|
||
void GoodGay::visit()
|
||
{
|
||
cout << "好基友类正在访问:" << building->m_SittingRoom << endl;
|
||
|
||
cout << "好基友类正在访问:" << building->m_BedRoom << endl;
|
||
|
||
}
|
||
|
||
void test01()
|
||
{
|
||
GoodGay gg; //创建一个GoodGay的对象,会调用GoodGay构造函数,会创建一个building,然后调用building构造函数
|
||
gg.visit();
|
||
}
|
||
|
||
int main()
|
||
{
|
||
test01();
|
||
|
||
system("pause");
|
||
|
||
return 0;
|
||
|
||
}
|
||
```
|
||
|
||
运行结果:
|
||
- 好基友类正在访问:客厅
|
||
- 好基友类正在访问:卧室
|
||
- 请按任意键继续. . .
|
||
|
||
# 3. 成员函数做友元
|
||
|
||
|
||
```python
|
||
#include <iostream>
|
||
using namespace std;
|
||
|
||
class Building;
|
||
class GoodGay
|
||
{
|
||
public:
|
||
GoodGay();
|
||
|
||
void visit(); //让visit函数可以访问Building中私有成员
|
||
void visit2(); //让visit2函数不可以访问Buildinng中私有成员
|
||
|
||
Building* building;
|
||
};
|
||
|
||
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()
|
||
{
|
||
building = new Building;
|
||
}
|
||
|
||
// 类外实现成员函数
|
||
void GoodGay::visit()
|
||
{
|
||
cout << "visit 函数正在访问" << building->m_SittingRoom << endl;
|
||
cout << "visit 函数正在访问" << building->m_BedRoom << endl;
|
||
|
||
}
|
||
|
||
void GoodGay::visit2()
|
||
{
|
||
cout << "visit2 函数正在访问" << building->m_SittingRoom << endl;
|
||
//cout << "visit 函数正在访问" << building->m_BedRoom << endl;
|
||
|
||
}
|
||
|
||
void test01()
|
||
{
|
||
GoodGay gg;
|
||
gg.visit();
|
||
gg.visit2();
|
||
}
|
||
|
||
int main()
|
||
{
|
||
test01();
|
||
|
||
system("pause");
|
||
|
||
return 0;
|
||
|
||
}
|
||
```
|
||
|
||
运行结果:
|
||
- visit 函数正在访问客厅
|
||
- visit 函数正在访问卧室
|
||
- visit2 函数正在访问客厅
|
||
- 请按任意键继续. . .
|