2, try to replace the # define with Const,enum,inline
(1) #define在预处理阶段被处理而const在便一阶段处理, if the name of the macro defined in the compilation phase is removed, the compilation will be error-prone and not easy to tune.
(2) You cannot create a class-specific constant with # define because # define does not value scopes.
Class Costestimate{private: static const Double Fudgefactor;//static class constant declaration in header file ... } Const double Costestimate::fudgefactor = 1.35;//static CLASSS constant defined in the implementation file
(3) When a constant value is required during class compilation, if your compiler does not allow the static integer class constant to set the initial value within class, you can use the "enum hack" compensation method
Class Gameplayer{private: enum{numturns = 5}; int scores[numturns];};
(4) For macros with criminal functions, it is best to replace # define with the inline function instead.
3. Use const whenever possible
(1) Keyword const VERSATILE
Char greeting[] = "Hello"; char* p = greeting; Non-const pointer,non-const Dataconst char*p=greeting; Non-const pointer,const datachar* const p=greeting; Const POINTER,NON-CONST dataconst char* const p=greeting; Const POINTER,CONST Data
Const Std::vector<int>::iterator ITER = Vec.begin (); ITER acts like a t* const*iter = 10; No problem, change the thing that ITER refers to ++iter; Error! ITER is Conststd::vector<int>::const_iterator citer=vec.begin (); The function of Citer is like a const t**citer=10; Error! *citer is constciter++; No problem, change citer.
(2) member functions
The purpose of the const implementation with the member function is to confirm that the member function can be used on the const object.
When the const and NON-CONST member functions have a substantially equivalent implementation, making the NON-CONST version call the const version avoids code duplication.
3. The object is initialized before it is used.
Initializing sequencing across compilation units
Class filesystem{ //From your library public: ... std::size_t numdisks () const; ......}; extern FileSystem TFS; The object that is intended for use by the client class directory{ //is established by the Library client public: Directory (params); ...};D irectory::D irectory (params) { .... std::size_t disks = Tfs.numdisks (); Use TFS ...} Further assuming that these customers decide to create a directory object, use temporary files: directory tempdir (params);//directories for temporary files
The importance of the initialization order is now apparent: unless TFS is initialized before TempDir, the TempDir constructor uses TFS that has not yet been initialized. But TFS and TempDir are different people in different
Time is established in different code files, they are non-local static objects defined in different compilation units, how can I be sure that TFS will be initialized before TempDir?
Use local static instead of non-local static
Class filesystem{...}; Previous filesystem& TFS ()// This function is used to replace TFS object; it may be a static{ static FileSystem FS in FileSystem class; Define and initialize a local static object return FS; Returns a reference pointing to the above object}class directory{...};//previous Directory::D irectory (params) { ... std::size_t disks = TFS (). Numdisk (); The original reference to TFS now changes to TFS () ...} directory& TempDir (params)//This function is used to replace the TempDir object, which may be a static{ static directory TD within the Directory class; Define and initialize the local object return TD; Returns a reference pointing to the above object}
Please remember:
(1) Manual initialization for built-in objects, because C + + does not guarantee initialization of them.
(2) constructors are best used to initialize columns with members, rather than using assignment operations within constructors. The initial Value column lists the member variables in the same order as they are declared in class.
(3) In order to exempt the "initialization Order of cross-compilation units" issue, replace the non-local static object with the local static object.
Make yourself accustomed to C + +