學習日誌—給C程序員的建議

爲了幫助自己區分之前學習C語言:

 

[1]在C++裏幾乎不需要用宏。consatenum定義明顯的常量,用inline避免函數調用的額外開銷,用template去刻畫 families of functions of types, and namespace to avoid  name clashes.

 

[2]不要在你需要變量之前去聲明它,以保證你能立即對它進行初始化。A declaration can occur anywhere  a statement can

,in for-statement initializers,and in conditions.

 

[3]Don't use malloc(). The new operator does the same job better , and instead of realloc() , try  a vector().

 

[4]Try to avoid  void* , 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 coversation , try using  one  of  the "new cast" for a more precise statement of  what you are trying to do.

 

[5]Minimize the use of arrays and C-style strings. The C++ standard library string and vector classes can often be used to simplify programming compared to traditional C style.In general ,try not to build yourself what has already been provided by the standard library.

 

Most important ,try thinking of a program  as a set of interacting concepts represented as classes and objects, instead of as a bunch of data structures with functions twiddling their bits.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章