您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐