Effective C++条項1~4
4533 ワード
条項1:認知C++C++拡張templates exceptions overloading virtual inheritance containers iteratorsはC++を一つの言語として連邦に多くの言語の特性を含んでいる
条項2:define defineの置き換えの欠点:定義された変数をプリプロセッサに置き換えると、object codeが複数回発生する可能性があります.
条項3:できるだけconst use const whenever possibleを使用する
条項4.オブジェクトの使用前に初期化された原則を決定します.intなどの組み込みタイプ、*Pは使用前に初期化する必要があります.その他の状況はコンストラクション関数に任せて完了します.付与値と初期化を区別し、コンストラクション関数のmember initialization listを使用して他のモジュールを初期化したオブジェクトがno local staticオブジェクトを使用している場合、初期の順序を確保するにはどうすればいいですか?P33
条項2:define defineの置き換えの欠点:定義された変数をプリプロセッサに置き換えると、object codeが複数回発生する可能性があります.
.const
const head define
1.const
const T* t;
T const* t;
T* const t;
const T* const t;
2.class
class const static ?static ?
static const int num=5; //static
: staic :P21
.enum hack
.inline
const enums define
inline #defines passed by reference-to-const
条項3:できるだけconst use const whenever possibleを使用する
1.const global namespace
2.class static no-static 。 static const ?
3.const
4.const == P19
5.const
const const
const char& operator[](std::size_t position)const{
return text[position]
}
// const cons
// passed by reference-to-const
//mutable class const , const
6.no-const
class TextBlock{
public:
const char& operator[](std::size_t position)const{
//dosomething
return text[position];
}
char& operator[](std::size_t position){
return const_cast<char&>(
static_cast<const TextBlock&>(*this)
[position]);
}
// : no-const const ,
// ? operator[]?
//1.*this const TextBlock& 2. const char& const
static_cast(obj) const 。
const_cast(obj) const
条項4.オブジェクトの使用前に初期化された原則を決定します.intなどの組み込みタイプ、*Pは使用前に初期化する必要があります.その他の状況はコンストラクション関数に任せて完了します.付与値と初期化を区別し、コンストラクション関数のmember initialization listを使用して他のモジュールを初期化したオブジェクトがno local staticオブジェクトを使用している場合、初期の順序を確保するにはどうすればいいですか?P33