Effective C++中的条款

改变旧有的C习惯:(shifting from C to C++)
1. 尽量以const和inline取代#define(prefer const and inline to define)
2. 尽量以<iostream>取代<stdio.h>(prefer iostream to stdio.h)
3. 尽量以new和delete取代malloc和free(prefer new and delete to malloc and free)
4. 尽量使用C++风格的注释(prefer C++ style commnents)
内存管理:(memory management)
5. 使用相同形式的new和delete(Use the same form in corresponding uses of new and delete)
6. 记得在destructor中以delete对付pointer member(Use delete on pointer member in destructors)
7. 对内存不足的状况预做准备(Be prepared for out-of-memory conditions)
8. 撰写operator new和operator delete是应遵循公约(Adhere to convention when writing operator new and delete)
9. 避免遮掩了new的正规形式(Avoid hiding the new)
10. 如果写了一个operator new对应的也要写一个operator delete(Write operator delete if you write operator new)
构造函数,析构函数和Assignment运算符:(Constructors, Destructors, and Assignment Operators)
11. 如果class内动态配置有内存,请为此class声明一个copy constructor和一个assignment运算符(Declare a copy constructor and an assignment operator for class with dynamically allocated memory)
12. 在constructor中尽量以initialization动作取代assignment动作(Prefer initialization to assignment in constructors)
13. initialization list中的members初始化次序应该和其在class内的声明次序相同(List members in an initialization list in the order in which they are declared)
14. 总是让base class拥有virtual destructors(Make sure base class have virtual destructors)
15. 令operator=传回“*this的reference”(Have operator= return a reference to *this)
16. 在operator=中为所有的data members设定(赋值)内容(Assign to all data members in operator=)
17. 在operator=中检查是否“自己赋值给自己”(Check for assignment to self in operator=)
类与函数的设计和声明:(Classes and Funcations: Design and Declaration)
18. 努力让接口完满且最小化(Strive for class interfaces that are complete and minimal)
19. 区分member functions,non-member functions和friend functions(Differentiate among member of functions, non-member functions, and friend functions)
20. 避免将data members放在公开接口中(Avoid data members in the public interface)
21. 尽可能使用const(Use const whenever possible)
22. 尽量使用pass-by-reference(传址)(Prefer pass-by-reference to pass-by-value)
23. 当你必须传回object时,不要尝试传回reference(Don't try to return a reference when you must return an object)
24. 在函数重载和参数缺省化之间,谨慎抉择(Choose carefully between function overloading and parameter defaulting)
25. 避免对指针型别和数值型别进行重载(Avoid overloading on a pointer and a numerical type)
26. 防卫潜伏的(模棱两可)状态(Guard against potential ambiguity)
27. 如果不想使用编译器暗自产生的member functions,就应该明白拒绝它(Explicitly disallow use of implicitly generated member functions you don't want)
28. 尝试切割global namespace(全局命名空间)(Partition the global namespace)
类与函数实现:(Classes and Functions: Implementation)
29. 避免传回内部数据的handles(Avoid returning)
30. 避免写出member functions,传回一个non-const pointer或reference并以之指向较低存取层级的members(Avoid member functions that return non-const pointer or references to members less accessible than themselves)
31. 千万不要传回“函数内local对象的reference”(Never return a reference to a local object or to a dereferenced pointer initialized by new with in the function)
32. 尽可能延缓变量定义式的出现(Postpone varible definitions as long as possible)
33. 明智的运用inlining(Use inlining judiciously)
34. 将文件之间的编译依赖关系降至最低(Minimize compilation dependencies between files)
继承关系和面向对象设计(Inheritance and object-Oriented Design)
35. 确定你的public inheritance模塑出“is-a”关系(Make sure public inheritance models)
36. 区分接口函数和实现继承(Differentiate between inheritance of interface and inheritance of implementaion)
37. 绝对不要重新定义继承而来的非虚拟函数(Never redefine an inherited nonvitual function)
38. 绝对不要定义继承而来的缺省参数值(Never redefine an inherited default parameter value)
39. 避免在继承体系中作向下的转型动作(Avoid casts down the inheritance hierarchy)
40. 通过layering技术来模塑has-a或is-implemented-in-terms-of的关系(Model)
41. 区分inheritance和templates(Differentiate between inheritance and templates)
42. 明智的运用私有继承(Use private inheritance judiciously)
43. 明智的运用多继承(Use muliple inhertance judiciously)
44. 说出你的意思并了解你所说的每一句话(Say what you mean; understand what you're saying)
杂项讨论:(Miscellany)
45. 清楚知道C++编译器为我们完成和调用那些函数(Know what function C++ silently writes and calls)
46. 宁愿编译和连接时出错也不要执行时出错(Prefer compile-time and line-time errors to runtime errors)
47. 使用non-local static objects之前先确定它已有初值(Ensure that non-local static objects are initialized before they're used)
48. 不要对编译器的警告视为不见(Pay attention to compiler warnings)
49. 尽量让自己熟悉C++标准库(Familiarize yourself with the standard library)
50. 加强自己对C++的了解(Improve your understanding of C++)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值