"Before object usage is determined, it is initialized." No doubt this is related to the C ++ constructor, before using the built-in types, the best way to ensure that they are initialized is to give them an initial value when defining a variable. The custom type is a class constructor, which is much more elegant than C.
Initialize the list of member variables of the class and initialize the function body. When talking about the version of the constructor, I think it is a bit inappropriate.
ABEntry: ABEntry (const std: string & name, const std: string & address,
Const std: list <PhoneNumber> & phones)
{
TheName = name;
TheAddress = address;
ThePhones = phones;
NumTimesConsulted = 0;
} When we use the constructor of the above value assignment version, we will first call the default constructor to initialize the class members and then assign them new values. I personally think there are several more questions:
1. When will the default constructor be called?
2. Why do I still call the self-written constructor after calling the default constructor?
3. Who calls the self-written constructor?
Compared to calling the default constructor for initialization and then assigning values, I personally think this interpretation is more reliable: when calling the constructor of the value assignment version, I first check whether the function has an initialization list, if there is an initialization list, the initialization is performed based on the values in the initialization list. If there is no initialization list, you can use the Default constructors of each member variable to initialize yourself. Then, call the value assignment operator in the function body to reload the function for the value assignment operation.
The above initialization method is certainly not recommended, because such constructor calls a value assignment operator overload function more than the constructor in the initialization list method. In terms of performance, this is unnecessary. So try to use the initialization list method.