Effective C++ 的笔记

写在前面

我觉得作为一个喜欢编程的人不会写C++好像有点说不过去。虽然我一直在写Java,然后也一直觉得C++写起来非常麻烦。不过就算是麻烦也应该先写一段时间再说对不对。至少C++是非常有趣,非常强大的,值得深入学习,虽然学不会。

这边是我记的Effective C++的笔记。有时间我再去看一看More Effective C++和C++ Primer, C++ Primer Plus。先记在计划里吧。

这本书里面介绍了55个守则,如果我们熟悉了这些守则,对我们在C++的开发是很有好处的,至少是前人总结的非常优秀的经验。我觉得一个写了三年多代码的人来看这本书还是比较轻松的,当然还是需要有一定的C++基础。Primer的书太厚了,一时半会儿看不完,有机会再看吧。

01: View C++ as a federation of languages 视C++为一个语言联邦

这是一个非常有趣的东西。我们可以使用很多种方式来使用C++,比如面向对象或者函数式编程…等。

这本书里把C++总结为了4个次语言组成的联邦:

  • C,也就是C++中C的部分
  • Object-Oriented C++
  • Template C++,我不是很懂这个。在Java里模板和面向对象是一起愉快使用的啊?
  • STL。是一个Template程序库

而我们在开发的时候总是选取了其中的某种次语言来进行,当我们在使用其中的某一个次语言的时候应该遵守相应的规范或者守则,尽管这可能会违背在另一个次语言中的规范。

02: Prefer consts, enums, and inlines to #defines 尽量以const, enum, inline替换#define

这个条款大意是,我们应尽量把事情丢给编译器而不是预处理器,因为defines只是一个在预处理器中的简单的替换。

比如如下的代码:

1
#define ASPECT_RATIO 1.653

每当使用ASPECT_RATIO的时候实际上进了编译器里都被替换为了1.653而不是同一个常量或者变量。

也就是编译器并不知道有一个记号名称叫ASPECT_RATIO。这里有一个记号表的概念我想是编译器里面的。有机会我应该会接触这个东西。

这个方式导致的问题可能是我们在获得一个编译错误的时候可能会感到很困惑,因为我们看见了一个没有名字的数值,这使人费解。

解决之道:

1
const double AspectRatio = 1.653;

注意这里的命名方式,一个常量的命名方式竟然是双驼峰。大写则是宏里面常用的方式。

(我有一个问题,类名的命名方式是怎么样的)

这么做比起用宏还有一个好处,可能会使代码的量更少。
可能一个浮点数使用的空间会比一个记号要来得更大一些,使用的浮点数宏越多,那么占用的空间也会越大。
如果宏里面是一个更大的东西那就更不用说了。

03:Use const whenever possible 尽可能使用const

04: Make sure that objects are initialized before they’re used 确定对象被使用前已先被初始化

05: Know that functions C++ silently writes and calls 了解C++默默编写并调用哪些函数

06: Expicitly disallow the use of compiler-generated functions you do not want 若不想使用编译器自动生成的函数,就该明确拒绝

07: Declare destructors virtual in polymorphic base classes 为多态基类声明virtual析构函数

08: Prevent exceptions from leaving destructors 别让异常逃离析构函数

09: Never call virtual functions during constructino or destruction 绝不在构造和析构过程中调用virtual函数

10: Have assignment operators return a reference to this 令operator=返回一个reference to this

11: Handle assignment to self in operator= 在operator=中处理“自我赋值”

12: Copy all parts of an object 复制对象时勿忘其每一个成分

13: Use objects to manage resources 以对象管理资源

14: Think carefully about copying behavior in resource-managing classes 在资源管理类中小心copying行为

15: Provide access to raw resources in resource-managing classed 在资源管理类中提供对原始资源的访问

16: use the same form in corresponding uses of new and delete 成对使用new和delete时要采取相同形式

17: Store newed objects in smart pointers in standalone statements 以独立语句将newed对象置入智能指针

18: Make interfaces easy to use correctly and hard to use incorrectly 让接口容易被正确使用,不易被误用

19: Treat class design as type design 设计class犹如设计type

20: Prefer pass-by-reference-to-const to pass-by-value 宁以pass-by-reference-to-const替换pass-by-value

21: Don’t try to return a reference when you must return an object 必须返回对象时,别妄想返回其reference

22: Declare data members private 将成员变量声明为private

23: Prefer non-member non-friend functions to member functions 宁以non-member、non-friend替换member函数

24: Declare non-member functions when type conversions should apply to all parameters 若所有参数皆需类型转换,请为此采用non-member函数

25: Consider support for a noon-throwing swap 考虑写出一个不抛异常的swap函数

艹, C++又把我看晕了