Item 18: Make interfaces easy to use correctly and hard to use incorrectly(Effective C++)
2011-03-24 18:09
501 查看
Good interfaces are easy to use correctly and hard to use incorrectly. Your should strive for these characteristics in all your interfaces.
Ways to facilitate correct use include consistency in interfaces and behavioral compatibility with built-in types.
Ways to prevent errors include creating new types, restricting operations on types, constraining object values, and eliminating client resource management responsibilities.
TR1::shared_ptr supports custom deleters. This prevents the cross-DLL problem, can be used to automatically unlock mutexes (see Item 14), etc.
1 #include <iostream>
2 #include <string>
3 #include <boost/shared_ptr.hpp>
4 #include <fstream>
5 using namespace std;
6
7 class Test
8 {
9 public:
void test(Test* t)
{
ofstream out("1.txt");
out << "test in class" << endl;
out.close();
}
};
void test(Test* t)
{
ofstream out("1.txt");
out << "test in global" << endl;
out.close();
}
void a()
{
Test* t = new Test();
//void (Test::*funcPtr)(Test*) = &Test::test;
boost::shared_ptr<Test> sp(t, test);
}
int main()
{
a();
cin.get();
return 0;
}
Ways to facilitate correct use include consistency in interfaces and behavioral compatibility with built-in types.
Ways to prevent errors include creating new types, restricting operations on types, constraining object values, and eliminating client resource management responsibilities.
TR1::shared_ptr supports custom deleters. This prevents the cross-DLL problem, can be used to automatically unlock mutexes (see Item 14), etc.
1 #include <iostream>
2 #include <string>
3 #include <boost/shared_ptr.hpp>
4 #include <fstream>
5 using namespace std;
6
7 class Test
8 {
9 public:
void test(Test* t)
{
ofstream out("1.txt");
out << "test in class" << endl;
out.close();
}
};
void test(Test* t)
{
ofstream out("1.txt");
out << "test in global" << endl;
out.close();
}
void a()
{
Test* t = new Test();
//void (Test::*funcPtr)(Test*) = &Test::test;
boost::shared_ptr<Test> sp(t, test);
}
int main()
{
a();
cin.get();
return 0;
}
相关文章推荐
- Effective C++ Item 18 Make interfaces easy to use correctly and hard to use incorrectly
- 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
- Effective Objective-C 2.0: Item 30: Use ARC to Make Reference Counting Easier
- Easy way to use JText 2.1.7 and HTML-style template for generating report
- I learned several ASP.NET's AJAX ability today! It is so interesting and so easy to use AJAX in ASP.NET.
- [翻译] Effective C++, 3rd Edition, Item 18: 使 interfaces(接口)易于正确使用,而难以错误使用
- Item 2: Prefer consts, enums, and inlines to #defines(Effective C++)
- Effective Java 18 Prefer interfaces to abstract classes
- how to write your annotation types and make use of built-in annotations to control their behavior
- Effective Java 19 Use interfaces only to define types
- The most simple and easy to use Qt GUI library plug-in FTStyle (2)The latest version - 2017.10
- Effective C# Item19: Prefer Defining and Implementing Interfaces to Inheritance
- Item 22: Prefer Defining and Implementing Interfaces to Inheritance(Effective C#)
- Item 13: Use objects to manage resources.(Effective C++)
- Effective Objective-C 2.0: Item 43: Know When to Use GCD and When to Use Operation Queues