Use Constructor Initializers

C++ Primer 4/e在Constructors这个地方有一个忠告:‘In many classes, the distinction between initialization and assignment is strictly a matter of low-level efficiency: A data member is initialized and assigned when it could have been initialized directly. More important than the efficiency issue is the fact that some data members must be initialized.

We must use an initializer for any const or reference member or for any member of a class type that does not have a default constructor.

By routinely using constructor initializers, we can avoid being surprised by compile-time errors when we have a class with a member that requires a constructor initializer.’

中文版的这样写:‘在很多classes中,初始化和赋值之间的区别仅仅在于效率:成员变数可被初始化,而初始化之后还可以被赋值。比效率重要的则是,某些成员变数一定得被初始化。

注意:对于const类型或reference类型的成员变数,或其class型别没有default建构式的成员变数,我们必须使用初值器。

如果能够养成使用建构式初值器的习惯,一旦我们的class有些成员变数需要建构式初值器,就不会大吃一惊地看到编译期错误。’

没有这个习惯,唉! 🙁