one, let oneself become accustomed to C + +
Item1. View C + + as a language Federation
C + + consists of the following parts: C, object-oriented C + +, Template C + +, STL
Item2. Try to replace #define with const, enum and inline.
Item3. Use const as much as possible
1. There are several kinds of const usages:
Char greeting[] = "Hello"; char *p = greeting; Non-const pointer,//NON-CONST data const char *p = greeting; Non-const pointer,//const data char * Const P = greeting; Const pointer,//NON-CONST Data const char * const P = greeting; Const pointer,//const data
In addition, the following two forms are the same:
void F1 (const Widget *PW); F1 takes a pointer to a
Constant Widget Object
void F2 (Widget const *PW); So does F2
Iterators also have a const:
Const Std::vector<int>::iterator ITER =//ITER acts like a t* const
Std::vector<int>::const_iterator citer =//citer acts like a const t*
2. Let the Non-const function call the const function to avoid repetition:
Const char& operator[] (std::size_t position) const//Same as before {... return text[position]; char& Operator[] (std::size_t position)//Now just calls Const op[] {return const_cast<char&> (//Cast Away const on Op[] ' s return type; Static_cast<const textblock&> (*this)//Add const to *this ' s type; [position]//Call const version of op[]); }
The const function makes it possible to call some methods (a const object can call only the Const function) by returning a const pointer, a const reference, and so on.
Item4. Make sure that the object is initialized before it is used
It is important not to confuse assignment (Assignment) with initialization (initialization), which is usually more efficient and initializes through a list of member initial values.
It is best to declare order as the order in the list of members.
An application of the Singleton pattern: initialization order for non-local static objects defined in different compilation units
Class FileSystem {...}; As before filesystem& TFS ()//This replaces the TFS object; It could is {//static in the FileSystem class static FileSystem FS;//define and initialize a local static object retur n FS; Return a reference to it} class Directory {...}; As before Directory::D irectory (params)//as before, except references to TFS are {//now to TFS () ... std::size_t D Isks = TFS (). Numdisks (); ... } directory& tempdir ()//This replaces the TempDir object; It {//could be-static in the directory class static directory TD;//Define/initialize Local static object return TD;// return reference to it}
That is, replace the non-local static object with a local static object
Ii. Construction, deconstruction, assignment operation
ITEM5. Understand what functions C + + silently wrote and called
These are generated by default (if you do not define): Copy constructs, copy assignment, destructors (non-virtual), default constructs, all of which are public and inline.
(Note: The above destructor (non-virtual), unless its base class is virtual destructor)
ITEM6. If you do not want to use a function that the compiler automatically generates, you should explicitly reject
Todo