標籤:
1、View C++ as a federation of languages。把C++看成4種子語言群組成,即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 are using.
因為C++有很多的編程範式,在項目開發過程中,明確規範怎麼使用C++很重要,這樣可以使整個團隊盡量使用一樣的風格,並把項目一直做下去
3、Prefer consts, enums, and inlines to #defines。prefer the compiler to the preprocessor。因為宏定義不會被編譯到符號表裡面去,因此會對問題的調試定位帶來麻煩,同時宏定義不能進行物件導向的封裝和私人化。
4、使用常量代替宏定義的時候需要注意兩個點,
一個是定義指標常量的時候需要使用兩個const,如
1 const char * const authorName = "Scott Meyers";
其中前一個const修飾char,後一個const修飾指標,可以逆著讀authorName 是一個const指標,指向const的char常量
另一個是使用class-specific constant的時候,為了保持一個記憶體copy,需要使用static進行定義
1 class GamePlayer {2 private:3 static const int NumTurns = 5; // constant declaration4 int scores[NumTurns]; // use of constant5 ...6 };
What you see above is a declaration for NumTurns, not a definition. Usually, C++ requires that you provide a definition for anything you use, but class-specific constants that are static and of integral type (e.g., integers, char s, bool s) are an exception. As long as you don‘t take their address, you can declare them and use them without providing a definition. If you do take the address of a class constant, or if your compiler incorrectly insists on a definition even if you don‘t take the address, you provide a separate definition like this:
1 const int GamePlayer::NumTurns; // definition of NumTurns; see 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 is initialized to 5 when it is declared), no initial value is permitted at the point of definition.(也可以聲明的時候不給初值,而在定義的時候給初值)
Note, by the way, that there‘s no way 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 }; // "the enum hack" — makes4 // NumTurns a symbolic name for 55 int scores[NumTurns]; // fine6 ...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 typically 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 your integral constants, an enum is a good way to enforce that constraint.
A second reason to know about the enum hack is purely pragmatic. Lots of code employs it, so you need to recognize it when you see it. In fact, the enum hack is a fundamental technique of template metaprogramming
6、another common (mis)use of the #define directive is using it to implement macros that 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))
替換為
1 template<typename T> // because we don‘t2 inline void callWithMax(const T&a,const T&b)// know what T is, we3 { // pass by reference-to-4 f(a > b ? a : b); // const — see Item 205 }
7、Given the availability of 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++〉讀書筆記--Accustoming Youself to C++