Article 04: Determine if object is initialized before use
1, because C part of C + + and initialization may result in a run-time cost, then there is no guarantee of initialization, for example, Arry is C parts of C + + is not guaranteed initialization, and the STL vector can guarantee initialization
2, because the rules are complex and changeable, so the best way is: Forever before using the object to initialize it
3, because the C + + Rule Object Member initialization action occurs before entering the constructor body, so in the constructor function, the given member initial value is not the member variable initialization, but the assignment action. (c + + class member variable initialization occurs when the default constructor of these member variables is called automatically, but this rule does not apply to C built-in types, because built-in types do not have a default constructor)
classPhoneNumber {...};classAbentry { Public: Abentry (ConstSTD::string&name,ConstSTD::string&address,ConstStd::list<phonenumber> &phones);Private: std::stringthename; STD::stringtheaddress; Std::list<PhoneNumber>Thephones; intnumtimesconsulted;}; Abentry::abentry (ConstSTD::string&name,ConstSTD::string&address,ConstStd::list<phonenumber> &phones) {thename= name;//Note: This is already an assignment, not an initializationTheaddress =address; Thephones phones; Numtimesconsulted=0;} Abentry::abentry (ConstSTD::string&name,ConstSTD::string&address,ConstStd::list<phonenumber> &phones): thename (name), theaddress (address), thephones (phones), numtimesconsulted (0){}View Code
4, using the initialization list is equivalent to using only one copy function, and using the constructor given the initial value is equivalent to calling the constructor of the member variable and then calling copy assignment
5. Const and reference member variables must be initialized with initialization list (recommended: To avoid a distinction, you can always use the member initialization list to write the constructor, even if no parameter constructor is available)
6. C + + has a very fixed sequence of member initialization: base class is initialized earlier than derived classes, and class member variables are always initialized in their declaration order.
7, non-local static in the initialization sequence of different compilation units There is uncertainty, try to use local static instead of non-local static
Class&TFS () { static FileSystem fs; return fs; } class Directory {...} Directory::D irectory (params) { = TFS (). Numdisks (); }
Please remember:
1, manual initialization for built-in objects, because C + + does not guarantee initialization of them
2, the constructor is best to use the initialization list, do not assign in the constructor body, and the initialization list order should be consistent with the Order of declaration
3, in order to avoid the "collapse of the initialization sequence" problem, with local-static instead of non-local static
Article 05: Understand what functions are written and called by C + + by default
1. If you do not declare yourself, the compiler will automatically declare a copy constructor for the class, a copy assignment operator and a destructor, and the default constructor. (Note: Automatically declared functions are public and inline)
2, note that the copy assignment operator is not always generated, in the following three cases the compiler will refuse to generate the copy assignment operator automatically:
A, the class contains reference members (because C + + does not allow changing the reference variable)
b, the connotation of the const member variable
C, base class declares the copy assignment operator as private
Article 06: If you do not want to use the compiler automatically generated functions, it should be explicitly denied
1. Declare the copy function or copy assignment as private (note: Its member function and friend function can still be called)
2, inherit the base class such as Uncopyable
class uncopyable{pretected: uncopyable () {} ~uncopyable {}private: Uncopyable (const uncopyable&); Uncopyableoperator= (const uncopyable&); }
Effective C + + (ii)