More Effective C++ 35 条款

时间:2022-04-24 22:10:02

一、基础议题(basics)

条款1:仔细区别 pointers 和 references(Distinguish
between pointers and references)

一个基本的语法问题。

条款2:最好使用 C++ 类型转换运算符(Prefer C++-style casts)

C++的类型转换运算符安全,容易解析,分工精细,虽然要打多点字。

条款3:绝对不要以 polymorphically(多态)方式來处理数组(Never treat arrays
polymorphically)

数组都是单一类型的,好像没有必要用多态方式处理。array[i] 与 array[i+1]之间的偏移量是静态决议的,so,不要以多态方式处理。呃,如果是指针数组例外,声明一个基类指针数组,仍然可以完美地以多态方式处理。。。。咔咔。。。

条款4:非必要不使用 default
constructor(Avoid gratuitous default constructors)

很多类使用默认构造函数不能构造有意义的对象,所以应该避免。此处出现的问题是:没有默认构造函数不能使用动态分配数组,也就不能使用许多标准库的容器。

二、运算符(operators)

条款5:对类型转换函数保持警觉(Be wary of
user-defined conversion functions)

自定义类型转换函数容易引起一起很难发觉的运行错误,应该避免使用。使用重载运算符或函数以取代自定义类型转换函数。STL就是这样干的。

条款6:区别
increment/decrement 运算符的前序(prefix)和后序(postfix)型式(Distinguish between prefix
and postfix forms of increment and decrement operators)

不只是重载时语法的不同。还有效率的不同。前置的返回引用,后置的返回临时对象。这也意味后置的不能连续调用。

条款7:千万不要重载 &&, ||, 和 or,(逗号) 运算符(Never overload &&,
||, or ,)

条款8:了解各种不同意义的 new 和 delete(Understand the
different meanings of new and delete)

三、异常(Exceptions)

条款9:利用 destructors 避免资源泄漏(Use destructors to prevent
resource leaks)

在构造函数分配资源,在析构函数中释放资源。C++保证delete调用,异常抛出时会调用类析构函数释放资源

条款10:在 constructors 內阻止资源的泄漏(resource leaks)(Prevent resource leaks in
constructors)

条款11:禁止异常(exceptions)流出 destructors 之外(Prevent exceptions from
leaving destructors)虚构函数释放资源,前提是对象已经被构造。若constructors内抛出异常,此时对象尚未构造,析构函数也就不会被调用。所以应该禁止异常流出构造函数。

条款12:了解「抛出一个异常」与「传递一个参数」或「调用一个虚拟函数」之间的差异(Understand how
throwing an exception differs from passing a parameter or calling a virtual
function)

条款13:以 by reference 方式捕捉异常 (exceptions Catch
exceptions by reference)

效率,效率,效率。。。。。。。

条款14:明智运用 exception
specifications (Use exception specifications judiciously)

条款15:了解异常处理(exception
handling)的成本 (Understand the
costs of exception handling)

四、效率(Efficiency)

条款16:謹記 80-20 法則(Remember the 80-20 rule)

80的时间用来运行20%的代码。所以优化应该针对那20%的代码。

条款17:考虑使用 lazy evaluation
(Consider using lazy evaluation)

条款18:分期摊还預期的计算成本(Amortize the
cost of expected computations)

条款19  了解临时对象的来源(Understand the origin of
temporary objects)

函数调用,返回时容易产生临时对象。

条款20:协助完成「返回值优化(RVO)」(Facilitate the return value
optimization)

条款21:利用多载技术(overload)避免隱式类型转换(Overload to avoid implicit
type conversions)

条款22:考虑以复合形式的运算符(op=)取代其型式(op) (Consider using op= instead
of stand-alone op)

原因很简单,op=不用返回临时对象。a=b+c+d+e;可转换为 ;   a=b; a+=c; a+=d; a+=e;

条款23:考虑使用其他程序库(Consider
alternative libraries)

条款24:了解 virtual
functions、multiple
inheritance、virtual base
classes、runtime type
identification 所需的成本(Understand the
costs of virtual functions, multiple inheritance, virtual base classes, and
RTTI)

关于vptr和vbtl。。。。。。太长,不写了。。。。

五、技术(Techniques,又称 Idioms 或 Pattern)

条款25:将 constructor 和 non-member functions 虚拟化(Virtualizing constructors
and non-member functions)

用一个成员函数将他们包装起来就行了。(visual consturtor 返回动态对象地址)

条款26:限制某个 class 所能产生的对象数量(Limiting the number of
objects of a class)

条款27:要求(或禁止)对象产生于 heap 之上(Requiring or prohibiting
heap-based objects)

条款28:Smart Pointers(灵巧指针)

条款29:Reference
counting(引用计数)

条款30:Proxy classes(代理类)

条款31:让函数根据一个以上的对象列别來決定如何虚拟化(Making functions
virtual with respect to more than one object)

模拟编译器多态的实现。

typedef pair ps;  map(string为所有对象类名,funtion pointer 为需要虚拟化的函数,静态成员函数)。然后用typeid取得两个参数对象的类名,再根据类名在map中取得正确的函数指针,调用。。。。。。咔咔,就是这么简单。

六、杂项讨论(Miscellany)

条款32:在未來时态开发程序(Program in the
future tense)

条款33:將非末端类(non-leaf classes)设计为抽象类(abstract classes)(Make non-leaf classes
abstract)

条款34:如何在同个程序結合 C++ 和 C (Understand how to
combine C++ and C in the same program)

条款35:让自己习惯使用标准的 C++ 語言(Familiarize yourself with
the language standard)