当前位置:网站首页>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 .
原网站

版权声明
本文为[zzyzxb]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270536495947.html