码迷,mamicode.com
首页 >  
搜索关键字:effective    ( 1955个结果
Effective C++ Item 38 通过复合塑模出 has-a 或 is-implemented-in-terms-of
经验:在应用域,复合意味着 has-a。 在实现域,复合意味着 is-implemented-in-terms-of 示例: template //将list应用于 Set。错误做法 class Set: public std::list {...}; 解析: public 继承表示 is-a,即如果D是一种B,对B为真的每一件事,对D也应该为真。但list可以包含相同的元素,而Set不可以 纠正: template class Set{ publi...
分类:编程语言   时间:2014-07-12 23:02:43    阅读次数:369
Effective C++ Item 37 绝不重新定义继承而来的缺省参数值
经验:绝对不要重新而来的缺省参数值,因为缺省参数值都是静态绑定,而 virtual 函数 -- 你唯一应该覆写的东西 -- 却是动态绑定 示例: class Shape{ public: enum ShapeColor {Red, Green, Blue}; virtual void draw(ShapeColor color = Red) const = 0; }; class Rectangle: public Shape{ public: virtual void draw(ShapeColor...
分类:编程语言   时间:2014-07-12 21:22:15    阅读次数:278
effective c++ 条款18 make interface easy to use correctly and hard to use incorrectly
举一个容易犯错的例子class Date{private: int month; int day; int year;public:Date(int month,int day,int year){ this->month = month; ... }}//wrong exampleDa...
分类:编程语言   时间:2014-07-12 00:13:54    阅读次数:235
Effective C++ Item 27 少做转型操作
旧式转型 (T) expression 或 T (expression) 新式转型 const_cast(expression) 通常被用来将对象的常量性转除(cast away the constness) dynamic_cast(expression) 执行“安全向下转型”,也就是用来决定某对象是否归属继承体系中的某个类型。 reinterpret_cast(expression) 执行低级转型 //不太懂?? static_cast(expression) 强迫隐式转换 ...
分类:编程语言   时间:2014-07-10 23:19:18    阅读次数:250
Effective C++ Item 28 避免返回对象内部数据的引用或指针
经验:避免返回handles(包括 references、指针、迭代器)指向对象内部。遵守这个条款可增加封装性, 帮助 const 成员函数的行为像个 const,并将发生“虚吊号码牌”(dangling handles)的可能性降至最低。 示例: class Point{ public: Point(int x, int y); //... void setX(int newVal); void setY(int newVal); //... }; struct RectData{ Point...
分类:编程语言   时间:2014-07-10 22:56:05    阅读次数:230
Effective C++ Item 30 inline里里外外
1.将大多数 inlining 限制在小型、被频繁调用的函数身上。这可使日后的调试过程和二进制升级更容易, 也可使潜在的代码膨胀问题最小化,使程序的速度提升机会最大化。 2.inline是对编译器的一个申请。 隐喻方式:将函数定义于 class 定义式内, 如成员函数或 friend 函数 明确声明:在定义式前加关键字 inline 编译器会拒绝大过复杂的函数 inlining, 而所有对 virtual 函数的调用也都会使 inlining 落空。 因为 virtual 意味 ”等待,直到运行期才确定...
分类:编程语言   时间:2014-07-10 20:49:39    阅读次数:205
Effective C++ Item 25 考虑写出一个不抛异常的swap函数
经验:当std::swap对你的类型效率不高时,提供一个swap成员函数,并确定这个函数不抛出异常 示例: stl里的swap算法 namespace std{ template void swap(T &a, T &b){ T temp(a); a = b; b = temp; } } //“pimpl手法”(pointer to implementation) --> 文件间的编译依存度 class WidgetImpl{ public: //... pr...
分类:编程语言   时间:2014-07-10 19:35:50    阅读次数:240
Effective C++ Item 26 尽可能延后变量定义式的出现时间
经验:尽可能延后变量定义式的出现。这样做可增加程序的清晰度并改善程序效率。 示例: //这个函数过早定义变量“encrypted” std::string encryptPassword(const std::string &password){ using namespace std; string encrypted; if(password.length() < MinimumPasswordLength){ throw logic_error("Password is too short"...
分类:编程语言   时间:2014-07-10 19:27:30    阅读次数:253
effective c++ 条款23 perfer nonmember nonfreind function to member function
主要的理由还是封装。nonmember nonfreind function 不能访问类private 成员变量。这个场景是有一个类提供了一些基本功能,比如class WebBrowser{ public: void clearCache(); void clearHist...
分类:编程语言   时间:2014-07-10 16:05:06    阅读次数:164
effective c++ 条款13 use object to manage resources.
请求的系统资源需要最终还回系统,为了避免遗忘返还这个动作,可以利用析构函数在object销毁时自动调用的特点来实现。简单说就是用object来管理资源。以内存资源为例class Investment {}; Investment* creatInvestment(){...} // factory ...
分类:编程语言   时间:2014-07-08 12:34:28    阅读次数:222
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!