当前位置:网站首页>Explain initialization list
Explain initialization list
2022-07-28 04:30:00 【Magic tea】
Catalog
3、 ... and : The calculation phase
Four : Initialization order of member variables
One : Definition
Let's start with an example
Date(int year, int month, int day)// Constructor with parameters
:_year(year)
, _month(month)
, _day(day)
{}This is a simple initialization list case , Start with a colon , Then there is a comma separated list of data members , Every " Member variables " Followed by one in the enclosure The initial value or expression in the number .
Two : characteristic
A. The list can only be initialized once , Multiple initialization will result in an error
Date(int year = 2022, int month = 4, int day = 19)// This is a general constructor
{
_year = year;
_month = month;
_day = day;
_day = day;// here _day Initialized twice , The compiler will report an error , Each member can only be initialized once
}B. The compiler also allows constructor initialization and initialization list initialization to be mixed
Date(int year = 2022, int month = 4, int day = 19)// There is no conflict between the two
: _year(year) // initialization
, _month(month)
{
_day = day;// Note that neither here nor above can be repeated twice , Such as _year(year). So it's repeated , Initialization can only occur once
}C.const Member variables 、 Reference member variables 、 Custom type members without a default constructor can only be initialized in the initialization list .
Date(int year = 2022, int month = 4, int day = 19)
: _year(year)
, _month(month)
, _n(2) //const Member variables must be initialized with an initialization list
{
_day = day;
//_n = 2; //const Member variables cannot be initialized in a function body
}
Date(int year = 2022, int month = 4, int day = 19)
: _year(year)
, _month(month)
,_ref(year)// Reference member variables should be initialized in the initialization list
{
_day = day;
//_ref = year; // A reference member variable cannot be initialized in a function body
}
Date(int year = 2022, int month = 4, int day = 19)
: _year(year)
, _month(month)
, _a(20)// Custom type member variables without a default constructor must be initialized in the initialization list
{
_day = day;
}
const Member variables 、 Reference member variables 、 The reason why a custom type member variable without a default constructor must be initialized in the initialization list :
1. The initialization list is where the object's member variables are defined .
2. The built-in type member variable of the object does not require initialization when initializing the list definition , Therefore, you can initialize in the initialization list , You can also initialize in the constructor body .
3.const Member variables 、 Reference member variables 、 A custom type member variable without a default constructor cannot be defined first and then initialized , They are defined in the initialization list , And must be initialized at definition time , Therefore, you must initialize... In the initialization list .
At the same time, the execution of constructor can be divided into two stages , Initialization phase and calculation phase , The initialization phase precedes the calculation phase .
Two : Initialization phase :
All class types (class type) All members of will be initialized in the initialization phase , Even if the member does not appear in the constructor's initialization list .
3、 ... and : The calculation phase
It is generally used to perform the assignment operation in the constructor body .
Four : Initialization order of member variables
class Date
{
public:
Date(int year, int month, int day)// Initialization is based on the order of definition , Such as year The first definition , Just the first initialization
:_month(month)// The second initialization
, _year(year) // First initialization
, _day(day) // The third initialization
{}
private:
int _year; // The first definition
int _month;// The second definition
int _day; // The third definition
};
边栏推荐
- 重要的 SQL Server 函数 - 数字函数
- Kingbasees Security Guide for Jincang database -- 6.1 introduction to strong authentication
- [kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]
- 虚拟机类加载机制
- High number_ Chapter 4__ curvilinear integral
- 【sylar】实战篇-基于 redis 的参数查询服务
- [practice] use the web animations API to realize a clock with accurate timing
- Reading the paper "learning span level interactions for aspect sentimental triple extraction"
- Information system project manager (2022) - key content: Project Procurement Management (12)
- Glusterfs file is not mounted, permission: R-S
猜你喜欢

Work fishing clock simulator wechat applet source code

idea2022更改本地仓库,配置阿里云中央仓库
![[yolov5 practice 5] traffic sign recognition system based on yolov5 -yolov5 integration pyqt5](/img/81/89b8e38801f706ef396943a79ef4c5.png)
[yolov5 practice 5] traffic sign recognition system based on yolov5 -yolov5 integration pyqt5

Citrix virtual desktop tcp/udp transmission protocol switching

空间复杂度计算超全整理!!(一起手撕复杂度计算

031 log4j log framework

校园流浪猫信息记录和分享的小程序源码

pytorch_ Lightning in lightning_ The output of hparams.yaml in logs is null

功耗:Leakage Power
![[mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]](/img/fd/6b261670c12e4d89c27364bcdf2a02.jpg)
[mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]
随机推荐
Kingbasees Security Guide for Jincang database -- 5.2. data integrity protection
20-Openwrt crond crontab
Information system project manager (2022) - key content: Project Procurement Management (12)
Efficient practice intelligent analysis of massive log data in aviation industry
CMake使用基础汇总
idea启动项目mvn命令终端用不了法将“mvn”项识别为 cmdlet
Detailed explanation of pl/sql parameters ("box model")
【sylar】框架篇-Chapter24-支持业务模块化
【二、移动web网页开发】2D&3D转换与动画、移动端布局、响应式布局
idea2022更改本地仓库,配置阿里云中央仓库
DNS series (III): how to avoid DNS spoofing
Kotlin——函数
Solana's "deceptive behavior": making mobile phones and opening stores
Kingbasees Security Guide for Jincang database -- 5.1. database transmission security
【sylar】框架篇-Chapter12-ByteArray 模块
26 openwrt port forwarding DMZ UPnP
Important SQL server functions - date functions
【sylar】框架篇-Chapter6-协程调度模块
Harmony's Application on the shelves reported an error. The solution of "please use the API of the released version to develop the application and apply for listing"
MySQL: data types and operators