update dir
This commit is contained in:
parent
71d83ce466
commit
f1e2b3fbef
@ -23,6 +23,7 @@
|
|||||||
- [x] [union那些事](./union)
|
- [x] [union那些事](./union)
|
||||||
- [x] [c实现c++多态那些事](./c_poly)
|
- [x] [c实现c++多态那些事](./c_poly)
|
||||||
- [x] [explicit那些事](./explicit)
|
- [x] [explicit那些事](./explicit)
|
||||||
|
- [x] [friend那些事](./friend)
|
||||||
|
|
||||||
代码运行:
|
代码运行:
|
||||||
全部在linux下用vim编写,使用gcc/g++调试!全部可正常运行!
|
全部在linux下用vim编写,使用gcc/g++调试!全部可正常运行!
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
- explicit 修饰转换函数时,可以防止隐式转换,但按语境转换除外
|
- explicit 修饰转换函数时,可以防止隐式转换,但按语境转换除外
|
||||||
|
|
||||||
|
|
||||||
代码参见:[./explict.cpp](./explicit.cpp)
|
代码参见:[.explicit.cpp](./explicit.cpp)
|
||||||
|
|
||||||
参考链接:
|
参考链接:
|
||||||
> https://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const
|
> https://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const
|
||||||
|
101
friend/README.md
Normal file
101
friend/README.md
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
# 友元函数与友元类
|
||||||
|
## 0.概述
|
||||||
|
|
||||||
|
友元提供了一种 普通函数或者类成员函数 访问另一个类中的私有或保护成员 的机制。也就是说有两种形式的友元:
|
||||||
|
|
||||||
|
(1)友元函数:普通函数对一个访问某个类中的私有或保护成员。
|
||||||
|
|
||||||
|
(2)友元类:类A中的成员函数访问类B中的私有或保护成员
|
||||||
|
|
||||||
|
优点:提高了程序的运行效率。
|
||||||
|
|
||||||
|
缺点:破坏了类的封装性和数据的透明性。
|
||||||
|
|
||||||
|
## 1.友元函数
|
||||||
|
|
||||||
|
在类声明的任何区域中声明,而定义则在类的外部。
|
||||||
|
|
||||||
|
```
|
||||||
|
friend <类型><友元函数名>(<参数表>);
|
||||||
|
```
|
||||||
|
|
||||||
|
注意,友元函数只是一个普通函数,并不是该类的类成员函数,它可以在任何地方调用,友元函数中通过对象名来访问该类的私有或保护成员。
|
||||||
|
|
||||||
|
具体代码见:[friend_func.cpp](friend_func.cpp)
|
||||||
|
|
||||||
|
```c++
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class A
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
A(int _a):a(_a){};
|
||||||
|
friend int geta(A &ca); ///< 友元函数
|
||||||
|
private:
|
||||||
|
int a;
|
||||||
|
};
|
||||||
|
|
||||||
|
int geta(A &ca)
|
||||||
|
{
|
||||||
|
return ca.a;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
A a(3);
|
||||||
|
cout<<geta(a)<<endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2.友元类
|
||||||
|
友元类的声明在该类的声明中,而实现在该类外。
|
||||||
|
|
||||||
|
```
|
||||||
|
friend class <友元类名>;
|
||||||
|
```
|
||||||
|
|
||||||
|
类B是类A的友元,那么类B可以直接访问A的私有成员。
|
||||||
|
|
||||||
|
具体代码见:[friend_class.cpp](friend_class.cpp)
|
||||||
|
```c++
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class A
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
A(int _a):a(_a){};
|
||||||
|
friend class B;
|
||||||
|
private:
|
||||||
|
int a;
|
||||||
|
};
|
||||||
|
|
||||||
|
class B
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int getb(A ca) {
|
||||||
|
return ca.a;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
A a(3);
|
||||||
|
B b;
|
||||||
|
cout<<b.getb(a)<<endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 3.注意
|
||||||
|
- 友元关系没有继承性
|
||||||
|
假如类B是类A的友元,类C继承于类A,那么友元类B是没办法直接访问类C的私有或保护成员。
|
||||||
|
|
||||||
|
- 友元关系没有传递性
|
||||||
|
假如类B是类A的友元,类C是类B的友元,那么友元类C是没办法直接访问类A的私有或保护成员,也就是不存在“友元的友元”这种关系。
|
BIN
friend/friend_class
Executable file
BIN
friend/friend_class
Executable file
Binary file not shown.
28
friend/friend_class.cpp
Normal file
28
friend/friend_class.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class A
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
A(int _a):a(_a){};
|
||||||
|
friend class B;
|
||||||
|
private:
|
||||||
|
int a;
|
||||||
|
};
|
||||||
|
|
||||||
|
class B
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int getb(A ca) {
|
||||||
|
return ca.a;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
A a(3);
|
||||||
|
B b;
|
||||||
|
cout<<b.getb(a)<<endl;
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
friend/friend_func
Executable file
BIN
friend/friend_func
Executable file
Binary file not shown.
33
friend/friend_func.cpp
Normal file
33
friend/friend_func.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/**
|
||||||
|
* @file friend_func.cpp
|
||||||
|
* @brief 友元函数
|
||||||
|
* @author 光城
|
||||||
|
* @version v1
|
||||||
|
* @date 2019-08-06
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class A
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
A(int _a):a(_a){};
|
||||||
|
friend int geta(A &ca); ///< 友元函数
|
||||||
|
private:
|
||||||
|
int a;
|
||||||
|
};
|
||||||
|
|
||||||
|
int geta(A &ca)
|
||||||
|
{
|
||||||
|
return ca.a;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
A a(3);
|
||||||
|
cout<<geta(a)<<endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user