学习C++的建议(Suggestions for learning C++)

来源:互联网 发布:淘宝层级是什么 编辑:程序博客网 时间:2024/04/30 08:17
  
学习C++的建议(Suggestions for learning C++)
——From:The C++ Programming Language
一、给C程序员的建议
Suggestions for C Programmers
[1] Macros are almost never necessary in C++. Use c o n s t or e n u m to define manifest constants,
i n l i n e to avoid function-calling overhead, t e m p l a t e s to specify families of functions and types, and  n a m e s p a c e s to avoid name clashes.
[2] Don’t declare a variable before you need it so that you can initialize it immediately. A declaration can occur anywhere a statement can, in for-statement initializers, and in conditions.
[3] Don’t use m a l l o c (). The n e woperator does the same job better, and instead of r e a l l o c (),
try a v e c t o r.
[4] Try to avoid v o i d *, pointer arithmetic, unions, and casts, except deep within the implementation of some function or class. In most cases, a cast is an indication of a design error. If you must use an explicit type conversion, try using one of the ‘‘new casts’’ for amore precise statement of what you are trying to do.
[5] Minimize the use of arrays and C-style strings. The C++ standard library s t r i n g and v e c t o rclasses can often be used to simplify programming compared to traditional Cstyle. In general, try not to build yourself what has already been provided by the standard library.
C++之父的建议
[1] C++里几乎不需要使用宏。用constenum定义明显的常量,用inline避免函数调用的额外开销,用template去刻画一族函数或者类型,用namespace去避免名字冲突。
[2] 不要在你需要变量之前去声明它,以保证你能立即对它进行去初始化。声明可以出现在能出现语句的所有未知上,可以出现在for语句的初始化部分,也可以出现在条件中。
[3] 不要用malloc( )new运算符能将同样的事情做得更好。对于realloc( ),请试一试vector( )
[4] 试着去避免void*、指针算术、联合和强制,除了在默写函数或类实现的深层之外。在大部分情况下,强制都是设计错误的指示器。如果你必须使用某个显示的类型转换,请设法去用一个“新的强制”,设法写出一个描述你想做的事情的更精确的语法。
[5] 尽量少用数组和C风格的字符串。与传统的C风格相比,使用C++标准库stingvector常常可以简化程序设计。
二、给C++程序员的建议
Advice
1When you program, you create a concrete representation of the ideas in your solution to some
problem. Let the structure of the program reflect those ideas as directly as possible:
[a] If you can think of ‘‘it’’ as a separate idea, make it a class.
[b] If you can think of ‘‘it’’ as a separate entity, make it an object of some class.
[c] If two classes have a common interface, make that interface an abstract class.
[d] If the implementations of two classes have something significant in common, make that
commonality a base class.
[e] If a class is a container of objects, make it a template.
[f] If a function implements an algorithm for a container, make it a template function implementing
the algorithm for a family of containers.
[g] If a set of classes, templates, etc., are logically related, place them in a common namespace.
2When you define either a class that does not implement either a mathematical entity like a
matrix or a complex number or a low-level type such as a linked list:
[a] Don’t use global data (use members).
[b] Don’t use global functions.
[c] Don’t use public data members.
[d] Don’t use friends, except to avoid [a] or [c].
[e] Don’t put a ‘‘type field’’ in a class; use virtual functions.
[f] Don’t use inline functions, except as a significant optimization.
C++之父的忠告
1在编程序时,你是在为你针对一些问题的解决方案中的思想建立一种具体表示。让程序的结构尽可能地反映这些思想
[a] 如果你能把“它”看成一个独立的概念,就把它做成一个类。
[b] 如果你能把“它”看成一个独立的实体,就把它做成某个类的一个对象。
[c] 如果两个类有共同的接口,将此接口做成一个抽象类。
[d] 如果两个类的实现有某些显著的共同点,将这些共性做成一个基类。
[e] 如果一个类是一种对象的容器,将它做成一个模板。
[f] 如果一个函数实现对某容器的一个算法,将它实现为对一族容器可用的模板函数。
[g] 如果一组类、模板等相互之间有逻辑联系,将它们放进一个名字空间里。
2在你定义一个并不是实现某个像矩阵或复数这样的数学对象的类、或者定义一个底层的类型比如链接表的时候:
[a] 不是使用全局数据(使用成员)。
[b] 不要使用全局函数。
[c] 不要使用公共数据成员。
[d] 不要使用友元,除非为了避免[a][c]
[e] 不要在一个类里面放“类型域”;采用虚函数。
[f] 不要使用内联函数,除非作为效果显著的优化。
注:“类型域”——指那种为了说明一个类所存储数据的情况而放置的标志域。
原创粉丝点击