当前位置:网站首页>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 )
边栏推荐
- laravel 中 distinct() 的使用方法与去重
- Linear regression with one variable
- [eye of depth wuenda machine learning homework class phase IV] summary of logistic regression
- 【量化投资系统】因子处理安装talib
- SQL Server enable CDC
- Binary search tree
- Codeforces Round #799 (Div. 4)
- [eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation
- qtcreator设置字符集
- JS to implement a detailed scheme for lazy loading of pictures (it can be used after being imported)
猜你喜欢

MySQL中有哪些约束?(实例验证)

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

征文投稿丨使用轻量应用服务器搭建博客环境
![[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC](/img/28/690f9985f32675f5d50d196c293abe.jpg)
[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC

关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知

Line features & surface features of vSLAM features

友元,静态关键字,静态方法以及对象间的关系

How to share the virtual environment of pycharm to jupyter Lab

JSP learning part

华为云的AI深潜之旅
随机推荐
SVM, problems encountered in face recognition and Solutions
嵌入式产品防盗版
[量化投资系统]Django从数据库中实现筛选及分页
Reflection perfectionism
Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3
C mqtt subscription message
穿越过后,她说多元宇宙真的存在
程序调试 - Debug/Release 版本
SVM,人脸识别遇到的问题及解决方法
Robotframework learning notes: introduction to robot framework and browserlibrary (playwright)
[eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation
js异或混淆代码
Improvement direction of programming ability
Common MySQL errors and solutions summarized painstakingly (II)
产品安全 - 小漏洞导致大问题
音视频开发案例99讲-目录
【量化投资系统】因子处理安装talib
Code:: blocks code formatting shortcuts
在iframe标签中操作外层dom
RobotFramework学习笔记:Robot Framework和BrowserLibrary(PlayWright)简介