高质量 C++/C 编程指南
文件标识:
当前版本: 1.0
作 者: 林锐 博士
文件状态
[ ] 草稿文件
[√] 正式文件
[ ] 更改正式文件
完成日期: 2001 年 7 月 24 日
高质量 C++/C 编程指南,v 1.0
2001 Page 2 of 101
版 本 历 史
版本/状态 作者 参与者 起止日期 备注
V 0.9
草稿文件
林锐
2001-7-1 至
2001-7-18
林锐起草
V 1.0
正式文件
林锐
2001-7-18 至
2001-7-24
朱洪海审查 V 0.9,
林锐修正草稿中的错误
高质量 C++/C 编程指南,v 1.0
2001 Page 3 of 101
目
目
录
录
前 言 .......................................................................................................................................6
第 1 章 文件结构....................................................................................................................8
1.1
版权和版本的声明........................................................................................................8
1.2
头文件的结构 ...............................................................................................................8
1.3
定义文件的结构 ...........................................................................................................8
1.4
头文件的作用 ...............................................................................................................8
1.5
目录结构 .......................................................................................................................8
第 2 章 程序的版式................................................................................................................8
2.1
空行...............................................................................................................................8
2.2
代码行 ...........................................................................................................................8
2.3
代码行内的空格 ...........................................................................................................8
2.4
对齐...............................................................................................................................8
2.5
长行拆分 .......................................................................................................................8
2.6
修饰符的位置 ...............................................................................................................8
2.7
注释...............................................................................................................................8
2.8
类的版式 .......................................................................................................................8
第 3 章 命名规则....................................................................................................................8
3.1
共性规则 .......................................................................................................................8
3.2
简单的 WINDOWS 应用程序命名规则 .........................................................................8
3.3
简单的 UNIX 应用程序命名规则..................................................................................8
第 4 章 表达式和基本语句....................................................................................................8
4.1
运算符的优先级 ...........................................................................................................8
4.2
复合表达式 ...................................................................................................................8
4.3
IF 语句...........................................................................................................................8
4.4
循环语句的效率 ...........................................................................................................8
4.5
FOR 语句的循环控制变量 ............................................................................................8
4.6
SWITCH 语句..................................................................................................................8
4.7
GOTO 语句......................................................................................................................8
第 5 章 常量............................................................................................................................8
5.1
为什么需要常量 ...........................................................................................................8
5.2
CONST 与 #DEFINE 的比较 ...........................................................................................8
5.3
常量定义规则 ...............................................................................................................8
5.4
类中的常量 ...................................................................................................................8
第 6 章 函数设计....................................................................................................................8
高质量 C++/C 编程指南,v 1.0
2001 Page 4 of 101
6.1 参数的规则 ...................................................................................................................8
6.2
返回值的规则 ...............................................................................................................8
6.3
函数内部实现的规则....................................................................................................8
6.4
其它建议 .......................................................................................................................8
6.5
使用断言 .......................................................................................................................8
6.6
引用与指针的比较........................................................................................................8
第 7 章 内存管理....................................................................................................................8
7.1 内存分配方式................................................................................................................8
7.2 常见的内存错误及其对策.............................................................................................8
7.3 指针与数组的对比 ........................................................................................................8
7.4 指针参数是如何传递内存的?.....................................................................................8
7.5
FREE 和 DELETE 把指针怎么啦?.................................................................................8
7.6
动态内存会被自动释放吗?........................................................................................8
7.7
杜绝“野指针” ...........................................................................................................8
7.8
有了 MALLOC/FREE 为什么还要 NEW/DELETE ?.......................................................8
7.9
内存耗尽怎么办?........................................................................................................8
7.10
MALLOC/FREE 的使用要点.........................................................................................8
7.11
NEW/DELETE 的使用要点 ...........................................................................................8
7.12
一些心得体会 .............................................................................................................8
第 8 章 C++函数的高级特性.................................................................................................8
8.1
函数重载的概念 ...........................................................................................................8
8.2
成员函数的重载、覆盖与隐藏....................................................................................8
8.3
参数的缺省值 ...............................................................................................................8
8.4 运算符重载 ...................................................................................................................8
8.5
函数内联 .......................................................................................................................8
8.6
一些心得体会 ...............................................................................................................8
第 9 章 类的构造函数、析构函数与赋值函数.....................................................................8
9.1
构造函数与析构函数的起源........................................................................................8
9.2
构造函数的初始化表....................................................................................................8
9.3
构造和析构的次序........................................................................................................8
9.4
示例:类 STRING 的构造函数与析构函数..................................................................8
9.5
不要轻视拷贝构造函数与赋值函数 ............................................................................8
9.6
示例:类 STRING 的拷贝构造函数与赋值函数 ..........................................................8
9.7
偷懒的办法处理拷贝构造函数与赋值函数.................................................................8
9.8
如何在派生类中实现类的基本函数 ............................................................................8
9.9
一些心得体会 ...............................................................................................................8
第 10 章 类的继承与组合......................................................................................................8
高质量 C++/C 编程指南,v 1.0
2001 Page 5 of 101
10.1 继承 .............................................................................................................................8
10.2
组合.............................................................................................................................8
第 11 章 其它编程经验 ..........................................................................................................8
11.1
使用 CONST 提高函数的健壮性..................................................................................8
11.2
提高程序的效率..........................................................................................................8
11.3
一些有益的建议..........................................................................................................8
参考文献 .................................................................................................................................8
附录 A :C++/C 代码审查表 ................................................................................................8
附录 B :C++/C 试题............................................................................................................8
附录 C :C++/C 试题的答案与评分标准.............................................................................8