The order in which class objects are constructed is this:
A. Allocating memory, when invoking constructors, implicitly/displayed initialization of individual data members
B. Perform a general calculation in the constructor after entering the constructor function
1. There are two ways to initialize a member of a class, one is to use an initialization list, and the other is to assign a value in the constructor body. the use of initialization lists is primarily based on performance issues. [Reference C + + initialization list http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html. ]
For built-in types such as int, float, etc., it is not very significant to use the initialization class table and initialize in the constructor body, but for class types it is best to use the initialization list. As the above test shows, the process of invoking the default constructor one less time with the initialization list is very efficient for data-intensive classes.
2. In addition to performance issues, some time-of-use initialization lists are essential, and the following conditions must be used to initialize the list
- Constant members, because constants can only be initialized and cannot be assigned, so they must be placed in the initialization list
- Reference type, the reference must be initialized at the time of definition and cannot be re-assigned, so it is also written in the initialization list
- There is no default constructor for the class type, because using an initialization list can be initialized without having to call the default constructor.
- If we have a class member that is itself a class or a struct, and this member has only one constructor with parameters and no default constructor, then to initialize the class member, you must call the constructor with the parameter of the class member, if there is no initialization list, Then he will not be able to complete the first step, will be error.
- It has been experimentally discovered that for inherited classes, if the parent class does not have a default constructor, the parent class member must be initialized in the constructor of the subclass to initialize the list, otherwise an error is made.
Base class:
classtime{ Public: Time (intHintm=0,ints=0) {cout<<"A construct"<<Endl; Hour=h; Min=m; SEC=s;} voidShow_time () {cout<":"<<min<<":"<<sec<<Endl;} intGethour () {returnHour;} intgetmin () {returnmin;} intgetsec () {returnsec;}protected: inthour;intmin;intsec;};
Child class, constructor declaration:
DTime (int h, int m, int s); DTime (Time &T);
classDTime: Publictime{ Public: DTime (intHintMints); DTime ( time&t); voidShow_time () {cout<<date<<"-"<":"<<min<<":"<<sec<<Endl;} voidSetdateintd) {Date=D;}Private: intdate;};
Use the initialization list when defining constructors:
The colon is later initialized with a comma-separated list of members
When initializing a base class, be aware that there must be a corresponding form of constructor defined in the base class, otherwise an error is given.
Error Example:
DTime::D Time (Time &t)
:d ate//error C2512: "Time": no appropriate default constructor is available
There is no default constructor in the Destructors class, and the base class member must be initialized with a custom base class constructor in the initialization list.
{
}
Correct example:
DTime::D time (int h, int m, int s)//This is consistent with declaration: Time (H,m,s)//base class initialization, date//member variable class
{
}
DTime::D Time (Time &T)//Here is consistent with the declaration
: Time (T.gethour (), T.getmin (), t.getsec ())//base class initialization
, date ()//Member initialization
{
}
DTime::D time (int h, int m, int s): Time (H,m,s), date () {} DTime::D Time (Time &T): Time (T.gethour (), T.getmin (), T.getsec ()), date (){}
Use:
int _tmain (int argc, _tchar* argv[]) {Time time (1); Time.show_time (); Time & t = time ; DTime DTime (t);//Here is consistent with the declaration dtime.show_time (); DTime d_time (a);//Here is consistent with the declaration d_time.show_time (); System ("pause"); return 0 ;} /* A construct1:0:0a construct2014-1:0:0a construct2014-12:24:25 Please press any key to continue ... */
Reference:
1. Summary of considerations related to "class" in C + + (11): Member Initialization list (★) http://www.cnblogs.com/charley_yang/archive/2011/04/07/2007356.html
2. C + + initialization list http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html
C + +, initialization list