Home > Back-end >  Members of the c initialization sequence problem
Members of the c initialization sequence problem

Time:10-11

Software college freshmen, beginner c + +, I hope god help me to solve a problem:
Why to set in the class definition in c + + member initialization sequence is determined by the statement order this rule??

CodePudding user response:

C + + great god is so of the regulation, you remember; Don't, so you want to such a sequence to initialize

CodePudding user response:

CodePudding user response:

If a member depends on another member order is not big problem?
Actually not provided global variable initialization sequence, C + + without regulation function parameter calculation sequence, some expressions calculating order, has caused many problems, (inherited from C)

CodePudding user response:

"C + + builder, a senior application development guide" on page 28, there are some content are for reference only
Members of the object initialization order
Used Fortan and Pascal programmers may like it very much lower bound and upper bound, arbitrary array in c + + is easy to implement this feature, a simulation is given below with arbitrary lower bound and upper bound of the array class, in general, this class also limited, why not use template to realize such a function? Before the question, carefully analyze the implementation of this is correct,
The class IntArray {
Public:
IntArray (int lowBound, int highBount);
.
Private:
The vector & lt; int> The data;
Int size;
Int lBound hBount;
};
IntArray: : IntArray (int lowBound, int highBound) : size (highBount - lowBound - 1), data (size)
{
}
It all looks very perfect, although there is no inspection on lower bound and upper bound of the array, to ensure that the lower bound must be less than the upper bound, but there is a mistake in the trouble, even if the lower bound of the array must be less than the upper bound, also absolutely can't know the size of the array,
Why is this so? All the members have been careful to initialize, isn't it? In fact, the class didn't do this, members are in the order they appear in the class to initialize, and in the list of initialization function of constructing and their order of appearance no relation, as a result, the upper class first using an uninitialized variable data parameters to construct a vector & lt; int> Object, next to integer variable initialization, it will bring very grave consequences, such as wonder: why is the computer's memory is so easy to run out? So please check carefully your initialization list, it would be better to prevention before the hidden trouble in work,
Conclusion is: no matter what time, are in strict accordance with the member variables under the order to arrange the initialization list, to ensure that won't accidentally made the serious mistake,
  • Related