当前位置:网站首页>Classes and objects - 5
Classes and objects - 5
2022-04-22 18:39:00 【Milu】
The first thing to look at is the last two default member functions : Access address and const Its address operator is overloaded
For members of ordinary objects, it is very simple to overload the address :

Direct implementation , And for the const We need to change the address overload of members of ordinary objects :

Now let's talk about const member ,const Modify the member function of a class : take const The member function of the modified class is called const Member functions ,const Modify class member functions , In fact, it is implicit to modify the member function this The pointer , Indicates that no member of the class can be modified in the member function . for instance :

We add... After the parameter const when , On behalf of const Decorated all the parameters in this member function , And all parameters of the function cannot be changed ( Change will report an error ), It is equivalent to protecting the program . Another example :

This should be well understood .
Be careful : For ordinary objects , You can call ordinary member functions , You can call const Member functions ; about const object , Can only call const Member functions , Normal member functions cannot be called . also const Member functions cannot call normal member functions ( May indirectly modify ), Ordinary member functions can call const Member functions . To use const Define member variables , Must be initialized .const The modifier is a constant , And replace it in the compilation stage .
And here comes the question , If we just want to change a parameter in a member function , Other parameters do not change , So what should we do ??
At this time, we will introduce keywords :mutable, When you start defining member variables of a class , You can add... To the front of the member variable to be changed mutable, In this class const In member function , We use it mutable The defined member variables can be modified .

Next, let's talk about the constructor mentioned before , We all know , When you create an object , The compiler calls the constructor , Give each member variable in the object an appropriate initial value ; however , Strictly speaking , This cannot be called initialization of class object members , It can only be called as the initial value given in the function body ( Initialization can only occur once , Assignment can be done many times ). therefore , Next, we introduce the concept of initialization list .
Initialization list : Start with a colon , Then a comma separated list of data members , Every “ Member variables “ Followed by the initial value or expression in parentheses , for example :

Be careful :
1. Each member variable can only appear once in the initialization list
2. Class contains the following members , Must be placed in the initialization list position to initialize : Reference member variables ,const Member variables , Custom type member variable
3. Try to initialize with an initialization list , Because whether you call the initialization list or not , For custom member variables , Will use the initialization list first
4. The order in which member variables are declared in a class is the order in which they are initialized in the initialization list ( It is independent of its order in the initialization list )
Next, let's talk about a keyword :explicit. Constructors can not only construct and initialize objects , For single argument constructors , And the role of type conversion , Let's take an example :

We actually feel strange at first , Why can an integer be assigned to a date class object ?? Actually d1=2021 The code logic of this step is , When the compiler comes to d1=2021 when , First use 2021 Call the one parameter constructor to create a temporary object , Then use temporary objects to dd Assign a value , Last call ~Date Destroy temporary objects .( Single argument constructors are not recommended ), When we use explicit When decorating a constructor , Will prohibit implicit conversion of single parameter constructors .
So if there's a need now : We need to know how many objects a class creates ? So how do we write ?
Let's look at the code first :

There will be problems when we debug , It is supposed to store an object created by ourselves , however d3 Two objects are created , So ordinary member variables don't work . Because each object of the ordinary member variable has a , And linked to specific objects , The objects we need to count should not be bound with objects , It should be shared by all objects .
Next, let's use global variables to see :

We can see , This result is in line with what we want , It is a variable to count all the objects of class price creation . however , Although the code is very simple and can achieve the desired effect , But the code is still unsafe , Global variables can be accessed arbitrarily by members in the whole project , And if we accidentally make a mistake , It may also lead to wrong results .
So we need a shared variable , It also introduces static member
static member , Declare as static A class member of a class is called a static member of a class , use static Modified member variables , It's called a static member variable ; use static Decorated member function , Called static member function . Static member variables are simply declared in a class , Need to be in Redefine outside the class ( No extra class static, But add the class name ::); Static member variables are not stored in the object , Not an attribute of a specific object , It all objects Common member variables ; Static member variables can be passed through objects , Static member variable name || Class name :: Static member variable name ; Static functions are restricted by access qualifiers ; Static function variables cannot be initialized at the constructor initialization position
Let's take the above code as an example :


If we want to call static member variables outside the class, we can write a function inside the class , Returns the value of a static member variable , Then call... Externally ( Static member variable sharing , You don't have to refer specifically to an object )

Be careful : In static functions, there is no this Pointer , That is, we cannot call non static member variables ; however , Ordinary member functions can call static member variables and functions . Static member functions cannot be set const Member functions ( Talked about before ,const The decoration is this The pointer ). Static member functions cannot be used as virtual functions ( The back can speak )
Okay , Today's content is here , I hope you can understand more after you go down , Watch more , Thank you for seeing the last !!!
版权声明
本文为[Milu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221727532092.html
边栏推荐
- 反射:newInstance()根据包名加类名得到具体类对象接口,调用指定类中的方法.
- 大话测试数据(一)
- DL之YoloV3:YoloV3论文《YOLOv3: An Incremental Improvement》的翻译与解读
- Esprima ECMAScript parsing architecture
- 花样机模板电脑CAD免费打板转格式软件PS300B使用步骤教程:日本兄弟牌Brother花样机通用CAD画图打板.dxf文件转.emb文件
- 100 Days of Code-day26(年月日转换的奥秘)
- B-tree [concept]
- [thinking and progress]: regret about yourself
- SegAttnGAN Text to Image Generation with Segmentation Attention
- Interpretation of the seven key tasks of the guiding opinions on comprehensively improving the development level of digital economy in Jiangsu
猜你喜欢
![[extensive reading of papers] eating embedded learning by comprehensive transcription of heterogeneous information networks](/img/d7/cb6fd42609cbf2104ef89cf8d9d5ba.png)
[extensive reading of papers] eating embedded learning by comprehensive transcription of heterogeneous information networks

在 Kubernetes 集群中部署现代应用的通用模式

Applets -- components

18730 coloring problem (two ways of writing fast power)

力扣-931.下降路径最小和

Pytoch Note58 CNN可视化

Leetcode 108: convert an ordered array into a binary search tree
![[fundamentals of interface testing] Chapter 11 | detailed explanation of postman associated interface and batch execution use case set](/img/e6/33b1cd576b08a5eb9ff455c20107ad.png)
[fundamentals of interface testing] Chapter 11 | detailed explanation of postman associated interface and batch execution use case set

Domestic chip dp9637-k bus transceiver replaces l9637d chip and si9241
![B-tree [concept]](/img/de/e52df3061a1615291de9b29c7b5c5c.png)
B-tree [concept]
随机推荐
文本分类问题中的一些指标
ZeroMQ:事件监控
Usage of SAP ABAP for all entries
How to select the mobile phone running memory?
SegAttnGAN Text to Image Generation with Segmentation Attention
Domestic chip dp9637-k bus transceiver replaces l9637d chip and si9241
[fundamentals of interface testing] Chapter 11 | detailed explanation of postman associated interface and batch execution use case set
【electron】文件下载卡住
图像的卷积——【torch学习笔记】
华为云媒体査勇:华为云在视频AI转码领域的技术实践
浅析局域网聊天软件的能力
力扣-931.下降路径最小和
关于.net core 中使用ActionFilter 以及ActionFilter的自动事务
Talk about the pit of cache Boolean value
高可用之——consul 文档
.net core 中使用IAsyncExceptionFilter 捕获全局异常,统一返回信息
PCB Layout Stackup setting
Hackmyvm (XXV) helium, series of articles continuously updated
【全网最全】JSR303参数校验与全局异常处理(从理论到实践别用if判断参数了)
Redis series (5) - common cache problems