Recently reading effective C + +, want to make some notes, the same as each module in the book, but it may be inconsistent with the specific order in the module. Will not be involved in every detail of the book, mainly write down what you feel important.
What is C + +?
C + + is a multi-paradigm programming language (MULTIPARADIGM programming language), one that supports both the process form (procedural), the object-oriented form (object-oriented), The language of the function form (functional), the generic form (genetic), and the meta-programming form (metaprogramming).
We can consider C + + as the main four sub-languages, including: C, object-oriented C + +, Template C + +, STL. (think this sort of good makes sense ...) Nonsense... )
Const those things
Syntax: If the keyword const appears to the left of the asterisk, indicating that the object is a constant, and if it appears to the right of the asterisk, the pointer itself is a constant, and if it appears on both sides of the asterisk, both the finger and the pointer are constants.
The STL iterator acts like a t* pointer, declaring that the iterator is const just like declaring a pointer to const (that is, declaring a t* const pointer), indicating that the iterator must not point to something different, but that the value of the object being referred to can be changed. Use Const_iterator If you want the iterator to refer to something that cannot be altered (that is, you want the STL to impersonate a const t* pointer).
Const Vector<int>::iterator ITER = Vec.begin (); The object is variable, the iterator is immutable vector<int>::const_iterator citer = Vec.begin ();//The object is immutable, the iterator is variable
Try not to define constants with #define, but with const variables.
When defining a constant pointer, it is necessary to declare the pointer (not just the pointer) as a const. (becomes a const pointer to const)
Class-specific constants should make them a static member of class, so that you can limit the scope of a constant and only one entity.
The function returns a constant value that reduces the surprises caused by customer errors without compromising security and efficiency.
For example, overloading the operator *, adding returns that are not const, may cause such an error.
if (a*b = c) ...//The comparison is mistakenly written as an assignment
Two member functions can be overloaded if they are just a constant variable. For example:
Const char& operator[] (size_t position) const {} char& operator[] (size_t position) {}
bitwise constness:
member functions can be said to be const only if they do not change any of the member variables of the object. Bitwise constness is the definition of C + + for constants.
The result is that many member functions do not have a const nature, but can be compiled. For example, char* belongs to an object and not to its point, so it can be modified by the pointer returned by the member function, which can be detected by the compiler.
logical constness:
A member function modifies some bits within the object it is working on, but only if the client is not able to detect it.
To modify data in a const member, you need to declare the variable as mutable. Mutable releases the bitwise constness constraint for the non-static member variable. For example: mutable size_t textLength;
The compiler enforces bitwise constness, but you should use "Conceptual constants" When you write your program.
the reuse of const and non-const
When the const and NON-CONST member functions have a substantially equivalent implementation, the NON-CONST version calls the const version code to avoid duplication.
Note: The const member function calls the NON-CONST member function as an error behavior.
Class textblock{public : const char& operator[] (std::size_t position) const { ...; ...; return text[position]; } char& operator[] (std::size_t position) { return const_cast<char&> ( static_cast< Const textblock&> (*this) [position] ); To read this conversion }}
Little Tips for functions
Parameter passing:
Pass-by-value is usually more efficient than pass-by-reference for built-in (aka C-like) types. In object-oriented C + +, because of the existence of user-defined constructors and destructors, Pass-by-reference-to-const is often better, as is Template C + +. For STL iterators and function objects, the legacy C Pass-by-value is more appropriate.
Inline: Use the inline function instead of a macro that resembles a function to avoid unnecessary errors.
Object initialization
Manual initialization of built-in objects, because C + + does not guarantee that they are initialized.
The constructor is best used with the member initial initial value column (member initialization list) instead of using an assignment operation (assignment) within the constructor body. The initial Value column lists the member variables in the same order as they are declared in class. Using member initial columns is sometimes absolutely necessary and often more efficient than assigning values.
The member variables of class are always initialized in the order in which they are declared. To avoid problems, the order in which you initialize them should be the same as the order in which they are declared.
To exempt from the initialization Order of cross-compilation units, replace the non-local static object with the local static object. Because C + + does not explicitly define the relative order of "non-local static objects defined in a different compilation unit", to eliminate this problem, you should design the following: Move each non-local static object into its own exclusive function, which is declared in this function as Static). C + + guarantees that the local static object within the function is initialized when the function is called "The first time a definition of the object is encountered."
Class filesystem{}; filesystem& TFS () { static FileSystem fs; return FS;} Class directory{...};D irectory::D irectory (params) { ...; std::size_t disks = TFS (). Numdisks (); ...;} directory& TempDir () { static Directory TD; return TD;}
However, any kind of non-const static object, whether it is local or non-local, in a multithreaded environment "Waiting for something to happen" will have trouble, one way to deal with this problem is:
All reference-returning functions are called manually during the single-threaded startup phase of the program (single-threaded startup portion), which eliminates the "race situation" (race condition) associated with initialization.
"Effective C + + notes" make yourself accustomed to C + +