当前位置:网站首页>Friends, static keywords, static methods, and relationships between objects
Friends, static keywords, static methods, and relationships between objects
2022-06-29 08:05:00 【Eat oranges aaaaa】
1 Friends
Friends friend The mechanism allows a class to authorize other functions to access its non-public members .
The friend declaration takes the keyword friend start , It can only appear in class declarations , They are not affected by their public private and protected Impact of zone .
Friends are divided into external function friends , Member function friend , Like friends .
Characteristics of friends :
- Without symmetry : A yes B Friends , It doesn't mean that B yes A Friends .
- It's not transitive : A yes B Friends ,B yes C Friends , but A No C Friends .
- Not inherited : Base Type inheritance Object type , If Object The type is A Friends , but Base The type is not A Friends .
1.1 External friend function :
If an external function wants to access private members of the an internal function , Must be defined as Friends form .
If you want to access in the main function value Value ,
The treatment is as follows :
5. You can add... To a class value Property value of is set to public ;
6. Change the attribute value through the object point ;
7. Print a Value 
Carry out friend 
1.2 Member function friend :
- Friend function is not a member function of a class , Accessing members of an object in the body of a function , You must add an operator to the object name "∵" Add object member name . But friend functions can access all members of the class , General functions can only access public members in a class .
- Friend functions are not restricted by the access key in the class , You can put it in the public of the class 、 private 、 The protection part , But the result is the same .
- The scope of a friend function of a class is not the scope of that class . If the friend function is a member function of another class , The scope is another — Scope of class , Otherwise, it is the same as the general function .

With Base Statement , But I don't know which attributes are included in the declaration , Will 12 Line function post compilation , Plus member function friends .
1.3 Like friends
An entire class can be a friend of another class . Each member function of a friend class is a friend function of another class , Can access all members in another class , share , Protect or private data members .
2 Static keywords
By keyword static Decorate a member of a class body , Become a static member of a class (static class member). A static member of a class is shared by all its objects , No matter how many objects there are , Only one copy of a static member is stored in public memory . Static data members are treated as global object pairs of this type .
- When adding static keywords to local variables , Visibility has not changed , Life span has changed .
- When adding static keywords to global variables , The lifetime has not changed , Visibility has changed ( Available only in this document , Not visible in other files )
2.1 Static attribute
In class design , With keywords static The decorated data members are static data members . All objects instantiated with this type , A storage space allocated by the shared system for static members , And this storage space is for program execution main Function , No longer allocate space for static members when instantiating objects ( Static member data is not in object space ).
2.1.1. Static data members are designed for information sharing ;

_pi For shared , therefore , Print out only 4 Bytes .
Example 2:
Example 3: Record the number of current objects created 
Special static members :
Example 3: In its own type design , Cannot be designed as either a value type or a reference . The type of a static data member can be its class, not static Data members can only be declared as pointers to this class .
summary :
- Static data members are designed for information sharing , And information exchange .
- Static data members of a class are shared by all class objects , It doesn't belong to a specific instance .
- Static data members of a class must be defined outside the class , Do not add... When defining static Keyword cannot be created in the initialization list of a constructor .
- The static data member type of the class is int,short, char, long long , And is const , Can be initialized directly in the class , It can also be initialized outside the class .
- Use static data members in member functions of classes , Static data members have not been previously this.
- When the static data members of a class are public, they can be used in external functions : Class name : Static data member name or object . Static data member name . You can define your own statically typed objects in the class body .
3 Static methods :
Function member description is static , Will be independent of the different objects of this class . Calls to static function members , Outside the object, you can do the following : Class name :: Function name or object name . Function name .
As opposed to static data members , For ease of use , Most static function members are public .
The static member function does not this The pointer , So in static member functions, you explicitly use this Pointers will cause compilation time errors . Attempt to access implicit use this The non static data member pointed to by the pointer will also cause compilation time errors .
summary :
A regular member function declaration describes three things that are logically different from each other :
- This function can access the private part of the class declaration .
- This function is in the scope of the class .
- This function must be activated via an object ( There is one this The pointer ).
Declaring a function as a friend can make it have only the first property .
Declare a function as static It can be made to have only the first and second properties
4 Object to object relationship
In a system , An object may be related to different objects , Here are the different relationships .
. rely on (Dependency)( Use one )
· relation (Association)( Use one )· polymerization (Aggregation)( There is one )
· Combine (Composition )( There is one ,“ use … To achieve ”)· Inherit (Inheritance)( It's a )
· Class template (Class template)
4.1 relation (Association): Use one
For two independent objects , Be the object A With another object B When there is a fixed correspondence , The two objects are related . An association is a special case of a dependency .
In type design , The association relationship is mainly reflected in the pointer or reference of the target type , As a property member of the current type . There is no relationship between whole and part , It just matters .
When designed as a pointer , Is a weak correlation ( A teacher can teach multiple courses at the same time ); When designed as a reference , For strong correlation ( Once designed , Then the teacher is bound to this course )
边栏推荐
- Electric check code configuration
- Prompt during packaging: property 'sqlsessionfactory' or 'sqlsessiontemplate'‘
- What are the constraints in MySQL? (instance verification)
- C编译器 - 隐式函数声明
- Detailed explanation of route (Jiuyang Scripture)
- Summary of array knowledge points
- 产品经理应该学习墨刀还是Axure?
- C#Mqtt订阅消息
- Pointer reference array element
- C compiler - implicit function declaration
猜你喜欢
![[eye of depth wuenda machine learning homework class phase IV] summary of logistic regression](/img/aa/62126bc7f5bbc0759ba3f4c847cf86.png)
[eye of depth wuenda machine learning homework class phase IV] summary of logistic regression

AC automata

Vulnhub's dc6 target

MongoDB-使用mongo/mongosh命令行连接数据库

VSLAM特征之线特征&面特征
![[Kerberos] analysis of Kerberos authentication](/img/c5/d429bcf3c26d9476531362ef286734.png)
[Kerberos] analysis of Kerberos authentication

Ceres optimization based on sophus

STM32基于HAL库的USART+DMA使用

ROS当中的仿真时间以及Bag包操作

Should product managers learn from ink knife or Axure?
随机推荐
自动化运维管理平台——spug的搭建和日常使用
Common MySQL errors and solutions summarized painstakingly (II)
Flutter shared_preferences使用
【修复收藏功能、更新登录接口】知识付费小程序、博客小程序、完整版开源源码、资源变现小程序,带299整站资源数据
js:Array. Reduce cumulative calculation and array consolidation
【6G】算力网络技术白皮书整理
C#Mqtt订阅消息
从Nacos客户端谈Nacos配置中心
laravel 中 distinct() 的使用方法与去重
Basics - syntax standards (ANSI C, ISO C, GNU C)
关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知
Electric check code configuration
产品安全 - 小漏洞导致大问题
Un voyage profond d'IA dans Huawei Cloud
Cartographer中的线程池操作
Product security - small vulnerabilities cause big problems
Reflection perfectionism
What are the constraints in MySQL? (instance verification)
JS XOR obfuscation code
PHP 7.1.13 版本,在使用过程中发现 浮点类型 数据经过 json_encode 之后会出现精度问题