当前位置:网站首页>12. constructor explanation, explicit, initialization list
12. constructor explanation, explicit, initialization list
2022-06-13 08:50:00 【zzyzxb】
One : Constructors : In class , There is a special member function , His name is the same as the class name , When we create the object of the class .
This special member function will be automatically called by the system . This member function , Call " Constructors ".
Because the constructor will be called automatically by the system , So we can simply understand it as : The purpose of a constructor is to initialize the data members of a class object .
class time{
public:
int hour;
int minute;
int second;
public:
time(int tmphour,int tmpmin, int tmpsec){
hour = tmphour;
minute = tmpmin;
second = tmpsec;
cout << " The constructor with three arguments is called " << endl;
}
public: time(){
hour = 12;
minute = 13;
second = 52;
cout << " An empty constructor was called " << endl;
}
public: time(int number){
hour = number ;
minute = 13;
second = 52;
cout << " Single argument constructor called " << endl;
}
public:
time(int tmphour,int tmpmin, int tmpsec=52){
hour = tmphour;
minute = tmpmin;
second = tmpsec;
};
(1) Constructor has no return value , This is also the special feature of constructors .
(2) Constructor cannot be called manually , Otherwise, the compilation will go wrong .
(3) Under normal circumstances , The constructor should be declared as public , Because when we create an object , The system calls the constructor for us , This shows that the constructor is a public function ,
Can be used by the system ( outside ) call . Because the default member of a class is a private member , So we must explain that the constructor is a public function , Otherwise, the object of this class cannot be created directly
(4) If there are multiple parameters in the constructor , Then we need to bring these parameters when creating the object .
time mytime = time(12,13,52); create a class object
time mytime2(12,13,52);
time mytime3 = time{12,13,52};
time mytime4{12,13,52};
time mytime5 = {12,13,52};
Two : Multiple constructors : There can be multiple constructors in a class , It can provide a variety of initialization methods for the creation of class objects . The number of parameters and the type of parameters are different .
time mytime = time(); create a class object
time mytime2;
time mytime3 = time{};
time mytime4{};
time mytime5 = {};
Object Copy
time mytime6; You can call the constructor
The following four objects do not call the traditional constructor , Follow up notes , What they call is actually a copy constructor .
time mytime7 = mytime6;
time mytime8(mytime6);
time mytime9{mytime6};
time mytime10 ={mytime6};
3、 ... and : Function default parameters
(1) Default values can only be placed in function declarations , Unless the function has no function declaration .
(2) When a default value is specified in a function with multiple arguments , All default parameters must appear to the right of non default parameters ,
Once a parameter begins to specify a default value , All parameters on its right must specify default values .
time mytime11 = time(12,13); create a class object
time mytime12(12,13);
time mytime13 = time{12,13};
time mytime14{12,13};
time mytime15 = {12,13};
Four : Implicit transformation and explicit
Compiling system , Did a lot of things we didn't know and didn't know in private .
time mytime40 = 14 The compiling system must have a behavior , hold 14 This number Transform to form a time type , Single argument constructor called .
time mytime = (12,13,14,15,16); Single argument constructor called .
void func1(time t1)
{
}
func1(16);
time mytime16 = {16}; We think this is normal , With one parameter 16, It can let the system know which constructor is called , Implicit type conversion .
time mytime17 = 16; Ambiguous writing , There is an implicit conversion of temporary objects .
Can the system be enforced , Explicitly require that constructors cannot do implicit type conversion ?
Sure , If the constructor declaration has explicit, Then this constructor can only be used for initialization and explicit type conversion .
class time1{
public:
int hour;
int minute;
int second;
public:
explicit time(int tmphour,int tmpmin, int tmpsec){
hour = tmphour;
minute = tmpmin;
second = tmpsec;
cout << " The constructor with three arguments is called " << endl;
}
};
time1 mytime = time1(12,13,52);
time1 mytime2(12,13,52);
time1 mytime3 = time1{12,13,52};
time1 mytime4{12,13,52};
time1 mytime5 = {12,13,52}; Implicit type conversion . There is a problem with this .
(1) For a single argument constructor , Generally stated as explicit , Unless you have a special reason .
5、 ... and : constructor initializer list
class time2{
public:
int hour;
int minute;
int second;
public:
time(int tmphour,int tmpmin, int tmpsec)
:hour(tmphour),minute(tmpmin),second(tmpsec)
{
cout << " The constructor with three arguments is called " << endl;
}
};
(1) Look professional , On the tall .
(2) More efficient .
边栏推荐
- JS - simple ATM of the for cycle case
- 5、 Constant, variable
- 【Pycharm踩坑记录】配置远程运行后提示,[Errno 2] No such file or directory
- 2020-12-28
- 国债逆回购能开户吗,国债逆回购在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
- In order to resist the flood, the soldiers have been fighting for 89 hours. How many days and hours are there in total?
- Process and scheduled task management
- 银行理财产品有哪些?清算期是多长?
- Installing pytorch under Anaconda
- filebeat采集日志到ELK
猜你喜欢

Installing pytorch under Anaconda

Use of addeventlistener in JS

File upload JS

關於RSA加密解密原理

Buffer Overflow Vulnerability Lab

Basic use of cesium, including loading images, terrain, models, vector data, etc

JS - for cycle case: Horse grain

Animation through svg

JS - simple ATM of the for cycle case

Container concept and cloud native
随机推荐
DIY UAV (anonymous controller p2+f330 rack)
ES6 use of dynamic attributes
Basic use of cesium, including loading images, terrain, models, vector data, etc
JD commodity detail interface, JD detail page interface, baby detail page interface, commodity attribute interface, commodity information query, commodity detail interface, H5 details, JD app details,
VI editor
How to save the video of wechat video number locally?
4. Relationship selector (parent-child relationship, ancestor offspring relationship, brother relationship)
[notes] like the solution to the problem of slow query (index + explicitly specifying query fields)
Buuctf web (VI)
Logstash failed to create queue
Namespace in TS (1)
Redis分布式集群搭建
Namespace in TS (2)
Input prohibit copy and paste
Gbase 8A disk problems and Solutions
Centering problem - the width and height of child elements are known
關於RSA加密解密原理
Knowledge points related to system architecture 2
Object array de encapsulation
Undefined and null in JS