Effective C ++ 2e Item13

zhaozj2021-02-11  190

Terms 13: The order of members listed in the initialization list and their order declared in the class

Stubborn Pascal and ADA programmers will often miss the functionality that can be marked with the lower limit of the array, that is, the range of subscripts of the array can be set to 10 to 20, not necessarily 0 to 10. Senior C programmer will insist on counting from 0, but I want to meet the requirements of those who are still using Begin / End, this only needs to define a self-owned Array class template:

Template Class Array {public: array (int lowbound, int highbound); ...

Private: Vector data; // A number of data stores in vector object / / About Vector Template See Terms 49

Size_t size; // The number of elements in arrays

INT LBOUND, HBOUND; // lower limit, upper limit};

Template Array :: Array (Int Lowbound, Int Highbound): Size (Highbound - Lowbound 1), LBound (Lowbound), HBound (Highbor), Data

The constructor will legally check the parameters to ensure that highbound is at least equal to Lowbound, but there is a very bad error here: even if the up and down limit of the array is legal, absolutely no one will know how many elements will be in Data.

"How is this possible?" I heard you called. "I carefully initialize Size to pass it to the constructor's constructor!" But unfortunately, you don't - you just want to do this, but not obey the rules of the game: class members are declared in the class The order is initialized, and they have no relationship with the order listed in the member initialization list. In the class generated by the Array Template above, DATA will always be initialized first, then Size, LBound, and HBound.

It seems that it seems that it is common, but it is reasonable. Look at this situation:

Class WACKO {public: WACKO (Const Char * S): S1 (s), S2 (0) {} WACKO (Const Wacko & RHS): S2 (rhs.s1), S1 (0) {}

PRIVATE: STRING S1, S2;

WACKO W1 = "Hello World!"; WACKO W2 = W1;

If the member is initialized in the order that appears on the initialization list, the order of the data members in W1 and W2 is created differently. We know that for all members of an object, their sequence of destructors is called, and they are always in the order in which they are created in the constructor. Then, if the above situation is allowed (ie, the member is initialized in the order in which they appear on the initialization list), the compiler will track the order of its members initialization for each object to ensure that their destructive functions are correct. The order is called. This will bring expensive overhead. Therefore, in order to avoid this opening, all objects of the same type are the same in the process of creating (constructed) and destroy (destructuring), regardless of the order in the initialization list.

In fact, if you are studying, you will find that it is just an initialization of non-static data members to comply with the above rules. The behavior of static data members is a bit like a global and namespace object, so it will only be initialized (see Terms 47). In addition, the base class data is always initialized before the derived class data, so when inheriting, the initialization of the base class is initialized at the forefront of the member initialization list. (If you use multiple inheritance, the base class is initialized in the order of initialization and the order inherited by the derived class, and their order in the member initialization list will be ignored. There are many inheritances to use many places to consider. Terms 43 About more inheritance should be considered What are the issues of suggestions.) Basic one is: If you want to figure out how the object is initialized, please make sure that the order in your initialization list and the order of members in the category declaration .

转载请注明原文地址:https://www.9cbs.com/read-4486.html

New Post(0)