學習
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++裡幾乎不需要使用宏。用const或enum定義明顯的常量,用inline避免函數調用的額外開銷,用template去刻畫一族函數或者類型,用namespace去避免名字衝突。[2] 不要在你需要變數之前去聲明它,以保證你能立即對它進行去初始化。聲明可以出現在能出現語句的所有未知上,可以出現在for語句的初始化部分,也可以出現在條件中。[3] 不要用malloc( )。new運算子能將同樣的事情做得更好。對於realloc( ),請試一試vector( )。[4] 試著去避免void*、指標算術、聯合和強制,除了在默寫函數或類實現的深層之外。在大部分情況下,強制都是設計錯誤的指標。如果你必須使用某個顯示的類型轉換,請設法去用一個“新的強制”,設法寫出一個描述你想做的事情的更精確的文法。[5] 盡量少用數組和C風格的字串。與傳統的C風格相比,使用C++標準庫sting和vector常常可以簡化程式設計。
二、給
C++
程式員的建議
Advice
1
、When you program, you create a concrete representation of the ideas in your solution to someproblem. 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 thatcommonality 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 implementingthe algorithm for a family of containers.[g] If a set of classes, templates, etc., are logically related, place them in a common namespace.
2
、When you define either a class that does not implement either a mathematical entity like amatrix 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] 不要使用內嵌函式,除非作為效果顯著的最佳化。註:“類型域”——指那種為了說明一個類所儲存資料的情況而放置的標誌域。