1. View C + + as a federation of Languages. Consider C + + as a 4 seed language, C, object-oriented C + +, Template C + +, the STL.
2, things to Remember:rules for effective C + + programming vary, depending on the part of C + + you is using.
Because C + + has a lot of programming paradigms, it is important to have a clear specification of how C + + is used in the project development process, so that the entire team can use the same style as possible and keep the project going.
3, Prefer consts, enums, and Inlines to #defines. Prefer the compiler to the preprocessor. Because macro definitions are not compiled into the symbol table, it can be problematic to debug the problem, while macro definitions are not object-oriented encapsulation and privatization.
4, the use of constants in place of macro definitions need to pay attention to two points,
One is to use two const when defining pointer constants, such as
1 const Char * const authorname = "Scott Meyers";
Where the previous const modifier char, after a const modifier pointer, can inverse read authorname is a const pointer to the const char constant
Another is when using Class-specific constant, in order to maintain a memory copy, you need to use static to define
1 class Gameplayer { 2 private : 3 static const int Numturns = 5 ; // constant declaration 4 int scores[numturns]; // use of constant 5 ... 6 };
What do you see above are a declaration for numturns, not a definition. Usually, C + + requires that's provide a definition for anything your use, but Class-specific constants that is static and of integral type (e.g., integers, char s, BOOL s) is an exception. As long as you don't take the their address, you can declare them and use them without providing a definition. If you don't take the address of a class constant, or if your compiler incorrectly insists on a definition even if you don ' t Take the address and you provide a separate definition like this:
1 const int gameplayer::numturns; // definition of numturns; 2 // below for why no value is given
Because the initial value of class constants is provided where the constant is declared (e.g., Numturns was initialized to 5 when it was declared), no initial value is permitted at the point of definition. (can also be declared when the initial value is not given, but at the time of the definition of the initial value)
Note, by the the-the-the-there ' no-to-create-a Class-specific constant using a #define, because #define s don ' t respect Scope.
5. The enum hack
1 class Gameplayer { 2 private : 3 enum {numturns = 5 }; // 4 Numturns a symbolic name for 5 5 int Scores[numturns]; // fine 6 ... 7 };
First, the enum hack behaves in some ways more like a #define than a const does, and sometimes that's what you want. For example, it's legal to take the address of a const, but it's not legal to take the address of an enum, and it's Typica Lly not legal to take the address of a #define, either. If you don't want to let people get a pointer or reference to one of the your integral constants, an enum is a good the to Enf Orce that constraint.
A second reason to know on the enum hack is purely pragmatic. Lots of code employs it, so you need to recognize it. In fact, the enum hack is a fundamental technique of template metaprogramming
6, another common (mis) use of the #define directive are using it to implement macros this look like functions but that don ' t Incur the overhead of a function call.
1 // call F with the maximum of A and B 2 #define Call_with_max (A, B) f (( a) > (b)? (a): (b))
Replaced by
1 // because we don ' t 2 void Callwithmax (const t&a,const t&b)// know what T is, we3 // pass by reference-to-4// const-see Item5 }
7, Given the availability of the const s, enum S, and inline s, your need for the preprocessor (especially #define) is reduced, But it's not eliminated. #include remains essential, and #ifdef/#ifndef continue to play important roles in controlling compilation. It's not yet time to retire the preprocessor, but you should definitely give it long and frequent vacations.
8, things to Remember:
? For simple constants, prefer const objects or enums to #define S.
? For Function-like macros, prefer-inline functions to #define S.
9, use const whenever possible
〈effective c++〉 Reading notes--accustoming youself to C + +