mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-10-28 19:04:45 +08:00
- const README: rewrite the space-saving claim per issue #5 follow-ups (constant-expression const vs #define immediates; which const qualify as constant expressions) - const README: fix the class-section diagnosis per issue #338 (overload resolution picks the const overload; the restriction only surfaces once that overload is removed) - sizeof README: virtual-inheritance result is compiler-dependent (GCC merges vbptr+vfptr -> 16, MSVC keeps them separate -> 32) #246 - static README: new section on internal linkage / access restriction with anonymous-namespace comparison #142 - 10-day practice: full problem list table + fixed day links #136 - new design_pattern/singleton/singleton_default.hpp: reusable thread-safe CRTP singleton template, compile+concurrency tested #303
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
# 友元函数与友元类
|
||||
> 本文档由 [C++那些事网站](https://light-city.github.io/stories_things/cpp-things/friend/) 同步生成,网站版本含多语言与更新排版。
|
||||
|
||||
# C++ friend 友元
|
||||
|
||||
## 关于作者:
|
||||
|
||||
个人公众号:
|
||||
|
||||

|
||||

|
||||
|
||||
## 0.概述
|
||||
|
||||
@@ -35,7 +37,7 @@ friend <类型><友元函数名>(<参数表>);
|
||||
|
||||
注意,友元函数只是一个普通函数,并不是该类的类成员函数,它可以在任何地方调用,友元函数中通过对象名来访问该类的私有或保护成员。
|
||||
|
||||
具体代码见:[friend_func.cpp](friend_func.cpp)
|
||||
具体代码见:[friend_func.cpp](https://github.com/Light-City/CPlusPlusThings/tree/master/basic_content/friend/friend_func.cpp)
|
||||
|
||||
```c++
|
||||
#include <iostream>
|
||||
@@ -74,7 +76,7 @@ friend class <友元类名>;
|
||||
|
||||
类B是类A的友元,那么类B可以直接访问A的私有成员。
|
||||
|
||||
具体代码见:[friend_class.cpp](friend_class.cpp)
|
||||
具体代码见:[friend_class.cpp](https://github.com/Light-City/CPlusPlusThings/tree/master/basic_content/friend/friend_class.cpp)
|
||||
```c++
|
||||
#include <iostream>
|
||||
|
||||
@@ -112,4 +114,4 @@ int main()
|
||||
假如类B是类A的友元,类C继承于类A,那么友元类B是没办法直接访问类C的私有或保护成员。
|
||||
|
||||
- 友元关系没有传递性
|
||||
假如类B是类A的友元,类C是类B的友元,那么友元类C是没办法直接访问类A的私有或保护成员,也就是不存在“友元的友元”这种关系。
|
||||
假如类B是类A的友元,类C是类B的友元,那么友元类C是没办法直接访问类A的私有或保护成员,也就是不存在“友元的友元”这种关系。
|
||||
Reference in New Issue
Block a user