看一遍忘一遍,觉得还是写点NOTES比较好。就从第四章开始吧。
Item 1: make interfaces easy to use correctly and hard to use incorrectly.
1, creating new types
(*Bad Practise*)
class Date {
public:
Date(int month, int day, int year);
...
};
(*Good practise*)
class Date {
public:
Date(const Month& month, const Day& day, const Year& year);
...
};
2, restrict operations on types, contrain object values
(*Bad Practise*)
struct Month {
explicit Month(int m):val(m){}
int m;
};
(*Good practise*)
(*This implementation is better than using enum which is not type safe*)
class Month {
public:
static Month Jan() {return Month(1);}
static Month Feb() {return Month(2);}
...
private:
explicit Month(int m):val(m){}
int m;
};
3, eliminating client resource management responsibility
(*Bad Practise*)
Investment* createInvestment();
(*Good practise*)
(*This implementation is better than using enum which is not type safe*)
std::tr1::shared_ptr createInvestment();
};