【Effective C++读书笔记】1. Accustoming Yourself to C++
添加时间:2013-7-23 点击量:
1. Item1: View C++ as a federation of languages
C++ is not a unified language with a single set of rules; its a federation of four sublanguages, each with its own conventions. The four sublanguages are:
(1) C.
(2) Object-Oriented C++: This part of C++ is what C with Classes was all about: classes, encapsulation, inheritance, polymorphism, virtual functions, etc.
(3)Template C++: This is the generic programming part of C++. Template is used a lot in C++.
(4)The STL: The STL is a template library with its own conventions. When you are working with the STL, you need to be sure to follow its own conventions.
Keep these four sublanguages in mind, and dont be surprised when you encounter situations where effective programming requires that you change strategy when you switch one sublanguage to another.
2. Item2: Prefer const, enum, and inline to #define
Using #define has many drawbacks. Therefore, we should use const, enum and inline instead of #define.
- For simple constants, prefer const objects or enums to #define.
- For function-like macros, prefer inline functions to #define.
3. Item3: Use const whenever possible
If the word const appears to the left of the asterisk, whats pointed to is constant; if the word const appears to the right of the asterisk, the pointer itself is constant; if const appears on both sides, both are constant.
const char p = greetings; //const data, non-const pointer
char const p = greetings; //const pointer, non-const data
const char const p = greetings; //const pointer, const data
char const p = greetings; //const pointer, non-const data(compare with the second one)
const Member Functions:
The purpose of const on member functions is to identify which member functions may be invoked on const objects. Non-const functions can only be invoked on non-const objects, while const functions can be invoked on both const and non-const functions.
Declaring something const helps compilers detect usage errors. Const can be applied to objects at any scope, to function parameters and return types, and to member functions as a whole.
4. Item4: Make sure that objects are initialized before theyre used
- Manually initialize objects of built-in type, because C++ only sometimes initializes theme itself.
- In a constructor, prefer use of the member initialization list to assignment inside the body of the constructor. List data members in the initialization list in the same order theyre declared in the class.
所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》
1. Item1: View C++ as a federation of languages
C++ is not a unified language with a single set of rules; its a federation of four sublanguages, each with its own conventions. The four sublanguages are:
(1) C.
(2) Object-Oriented C++: This part of C++ is what C with Classes was all about: classes, encapsulation, inheritance, polymorphism, virtual functions, etc.
(3)Template C++: This is the generic programming part of C++. Template is used a lot in C++.
(4)The STL: The STL is a template library with its own conventions. When you are working with the STL, you need to be sure to follow its own conventions.
Keep these four sublanguages in mind, and dont be surprised when you encounter situations where effective programming requires that you change strategy when you switch one sublanguage to another.
2. Item2: Prefer const, enum, and inline to #define
Using #define has many drawbacks. Therefore, we should use const, enum and inline instead of #define.
- For simple constants, prefer const objects or enums to #define.
- For function-like macros, prefer inline functions to #define.
3. Item3: Use const whenever possible
If the word const appears to the left of the asterisk, whats pointed to is constant; if the word const appears to the right of the asterisk, the pointer itself is constant; if const appears on both sides, both are constant.
const char p = greetings; //const data, non-const pointer
char const p = greetings; //const pointer, non-const data
const char const p = greetings; //const pointer, const data
char const p = greetings; //const pointer, non-const data(compare with the second one)
const Member Functions:
The purpose of const on member functions is to identify which member functions may be invoked on const objects. Non-const functions can only be invoked on non-const objects, while const functions can be invoked on both const and non-const functions.
Declaring something const helps compilers detect usage errors. Const can be applied to objects at any scope, to function parameters and return types, and to member functions as a whole.
4. Item4: Make sure that objects are initialized before theyre used
- Manually initialize objects of built-in type, because C++ only sometimes initializes theme itself.
- In a constructor, prefer use of the member initialization list to assignment inside the body of the constructor. List data members in the initialization list in the same order theyre declared in the class.