18. Make the interface easy to use problem:
- Parameters of the interface
- Interface relies on other calls
classDate {public: Date(intmonth,intdayintyear;}
The above code problem:
- Easy to pass parameters in the wrong order
- Invalid month or number of days may be passed
Workaround:
- Get prevention by importing new types
struct Day {explicit day ( int ): Val (d) {} int val;}; class date {public : Data (const month & M, const day & D, const year & y);}; date D (month (3 ), day (30 ), year (1995 ));
The above code cannot limit the data range
Workaround:
- Use enum to represent the month
- Pre-defined valid month
classMonthpublic: MonthMonth(1);} MonthMonth(2private: explicitMonth(int m); };Date d(MonthDay(30Year(1995
Interface design Principles
- Interfaces are not easily misused
- Create a new type
- Action on a restriction type
- Eliminate customer's responsibility for resource management
- Interface consistency
- STL containers have a size interface
- Do not have length, there is size
- Any interface if the customer must remember to do something, there will be incorrect use tendency
// 会导致忘记删除指针createInvestment();// 解决方法,保存到智能指针// 客户会忘记保存的到智能指针// 解决方法:函数返回智能指针createInvestment()
19. Design the class as if the type of design should be considered:
- How the new type object is created and destroyed
- constructors, destructors
- operator New,operator Delete, operator new[] operator delete[]
- What is the difference between object initialization and assignment of objects
- What does the object of the new type mean if it is pass by value?
- How the copy constructor implements
- What is the legal value of the newly-type
- Does the new type need to match an inheritance graph?
- If you inherit an existing class that is affected by that class, the effect of special virtual,non-virtual
- If other classes are allowed to inherit, affect the function you declare, especially if the destructor is virtual
- What kind of conversion does your type need
- T1 needs to be converted to T2,T1 write type conversion function operator T2, or T2 write Non-explicit constructor
- Whether the conversion requires a explicit function
- What operators and functions are reasonable
- Who takes the member with the new type
- Public,protected,private,friend
- How generalized is your new type?
- Do you need to have a class template
20. Use Pass-by-reference-to-const instead of Pass-by-value to pass-by-value problems
- Passed class object executes copy constructor, resulting in poor performance
- A member variable inside a class object executes the copy constructor, resulting in poor performance
Workaround:
- Passing constant references
- Passing a reference avoids cutting (the virtual function that the virtual function calls after the cut is the base class)
- Pass-by-value is more efficient for built-in objects
- The compiler puts built-in objects into the cache
- Small class objects, pass-by-reference more efficient
- The compiler may not optimize for small class objects
- Class object size can change as demand increases, becoming a large class object
Effective C + +-design and declaration