Effective C++ Item 18 Make interfaces easy to use correctly and hard to use incorrectly
2014-02-28 16:07
721 查看
1. A good API will provide easy to use interfaces but also provide hard to miss-use interfaces. Usually the later one is more fundamental than that of the previous one. Consider you want to write a data class, there are thousands ways to write it. Here is one example:
Both are OK, but only one is logical right.
Under such situation, a better way to implement that is to constrict clients what they can do and force them to right direction:
2. Let's see another example, suppose your interface returns a dynamic allocated resource and will be deleted after all. There are chances that a programmer will forget to delete it or delete it multi times. So, return a smart pointer would be a great idea.
Besides, smart pointer have another advantage that it will use default or assigned deleter, and you don't need to worry about "cross-DLL problems".
class Date { public: Date(int month, int day, int year); ... }; Date(3, 4, 2014); Date(4, 3, 2014);
Both are OK, but only one is logical right.
Under such situation, a better way to implement that is to constrict clients what they can do and force them to right direction:
class Month { public: static Month Jan() { return Month(1); } ... private: explicit Month(int m); }; Data d(Month::Mar(), Day(10), Year(2011));
2. Let's see another example, suppose your interface returns a dynamic allocated resource and will be deleted after all. There are chances that a programmer will forget to delete it or delete it multi times. So, return a smart pointer would be a great idea.
std::tr1::shared_ptr<Investment> createInvestment() { std::tr1::shared_ptr<Investment> retVal(static_cast<Investment>(0), getRidOfInvestment()); retVal = ...; //let retVal point to right object return retVal; }
Besides, smart pointer have another advantage that it will use default or assigned deleter, and you don't need to worry about "cross-DLL problems".
相关文章推荐
- Item 18: Make interfaces easy to use correctly and hard to use incorrectly(Effective C++)
- Effective C++ 18. Make interfaces easy to use correctly and hard to use incorrectly
- 条款18:让接口容易被正确使用,不易被误用(Make interface easy to use correctly and hard to use incorrectly)
- Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly
- Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly
- effective c++ 条款18 make interface easy to use correctly and hard to use incorrectly
- Item21 Perfer std::make_unique and std::make_shared to direct use of new
- How to Use C++ With PHP and JQuery to Make Neat Apps
- Effective C++ Item 16 Use the same form in corresponding uses of new and delete
- Effective C++ Item 13 Use object to manage resources
- effective C++ Item 2: Prefer consts, enums, and inlines to #defines
- <Effective Mordern C++>笔记:Item 8:prefer nullptr to 0 and NULL.
- Effective C++ Item 2:Prefer constS, enumS, and inlineS to #defineS
- <Effective Modern C++>Item 18: Use std::unique_ptr for exclusive-ownership resource management.
- Visual C++ Debugging: How to use 'ASSERT' and deal with assertions failures?
- Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
- A Fast and Easy to Use AES Library
- <Effective Modern C++>Item 15: Use constexpr whenever possible.[constexpr 表达式]
- Effective C++ Item 18 让接口容易被正确使用,不易被误用
- Effective Objective-C 2.0: Item 30: Use ARC to Make Reference Counting Easier