青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

平凡的世界

神鷹忽展翅,頭頂青天飛
隨筆 - 10, 文章 - 0, 評論 - 34, 引用 - 0
數據加載中……

[原][譯] Tips for Better Coding Style --- 關于更好的編程風格的建議

[C/C++] - Tips for Better Coding Style

關于更好的編程風格的建議 (v1.5)

Translated By Phoenix(phoenix8848@gmail.com)

    In this entry, I show you 4 tips that address frequently asked questions from C++ programmers of all levels of expertise. It's surprising to discover how many experienced programmers are still unaware of the deprecation of the .h notation of standard header files, the proper usage of namespaces, and the rules regarding binding of references to temporary objects, for example. These issues and others will be discussed here.
    在這篇文章里我將談談各種層次的C++程序員經常問及的四個問題。例如我很驚訝地發現還有很多程序員沒有意識到標準頭文件擴展名.h的爭議,命名空間的恰當用法以及引用臨時對象的規則。這些問題及其它將在這里進行討論。

    First, we start by explaining the difference between the deprecated “xxx.h” header names and the modern, standard-compliant “xxx” header-naming notation. Next, we explore a few dark corners of C++ which due to compilers' limitations and the somewhat recondite nature of the associated language rules tend(原文為“rulestend”) to confuse many programmers, e.g., the notion of comma-separated expressions and the rules of binding references to rvalues. Finally, we will learn how to invoke a function prior to a program's startup.

    首先我們從解釋受非議的“XXX.h”頭文件名與現代、符合標準的“<XXX>”頭文件名記號之間的區別開始。接下來我們探索C++不為人知的角落,由于編譯器的局限性和關聯語言規則某些隱蔽的自然特性迷惑了許多程序員,比如逗號分隔表達式的意義與引用型變量的規則。最后我們將學習如何在程序啟動前啟動一個函數。

    Tip 1: “iostream.h” or “iostream”?

    語題1:“iostream.h” or “iostream”?

    Many C++ programmers still use “iostream.h” instead of the newer, standard compliant “iostream” library. What are the differences between the two? First, the .h notation of standard header files was deprecated more than five years ago. Using deprecated features in new code is never a good idea. In terms of functionality, “iostream” contains a set of templatized I/O classes which support both narrow and wide characters, as opposed to “iostream.h” which only supports char-oriented streams. Third, the C++ standard specification of iostream's interface was changed in many subtle aspects. Consequently, the interfaces and implementation of “iostream” differ from those of “iostream.h”. Finally, “iostream” components are declared in namespace std whereas “iostream.h” components are global.

     很多C++程序員還在使用“iostream.h”代替新的符合標準的“iostream”庫。兩者有什么區別呢?首先,標準頭文件“.h”擴展名在五年前就倍受爭議。在新代碼中使用有爭議的(過時的)特性永遠都不是一個好主意。從本質上看,“iostream”包括一系列支持窄字符與寬字符的模板化(templatized) I/O輸入輸出類,而相反地,“iostream.h”只支持字符流。第三,iostream接口的標準C++規范在許多細節方面進行了變動。因此,“iostream”的接口與實現同那些“iostream.h”是有區別的。最后,“iostream”是在std命名空間中定義的而“iostream.h”則是全局的。

    Because of these substantial differences, you cannot mix the two libraries in one program. As a rule, use “iostream” unless you're dealing with legacy code that is only compatible with “iostream.h”.

    由于這些本質方面的不同,不能在同一程序中混合使用這兩種庫。作為一個規則,應盡量使用“iostream”,除非你要處理的是只能與“iostream.h”兼容的遺留代碼。(感謝autumnm1981

    Tip 2: Binding a Reference to an R-Value

    話題2:將引用與右值綁定

    (R-Value:右值,與“左值”相對。例如x=3中,“x”是一個“左值”,“3”是一個右值。從本質上講,左值是一個內存的地址,右值是一個實際的二進制值。)

    R-Values and L-Values are a fundamental concept of C++ programming. In essence, an R-Value is an expression that cannot appear on the left-hand side of an assignment expression. By contrast, an L-Value refers to an object (in its wider sense), or a chunk of memory, to which you can write a value. References can be bound to both R-Values and L-Values. However, due to the language's restrictions regarding R-Values, you have to be aware of the restrictions on binding references to R-Values, too.

    右值和左值是C++編程的一個基本概念。本質上來講右值是一個不可能出現在等號左邊的表達式。相反,左值引用一個對象(廣義范圍上的),或者一塊可讀寫的內存。引用既可以指向右值也可以指向左值。然而,由于語言在處理右值上的限制,你也得在將引用指向右值是慎重考慮。

    Binding a reference to an R-Value is allowed as long as the reference is bound to a const type. The rationale behind this rule is straightforward: you can't change an R-Value, and only a reference to const ensures that the program doesn't modify an R-Value through its reference. In the following example, the function f() takes a reference to const int:

    將引用與右值綁定像引用常量(這里const type應為"常量",感謝新奧爾良土鱉 )一樣也是被允許的。這條原則背后的原理是很顯而易見的:你無法改變右值,因為對常量的引用確保程序不會通過這個接口改變右值。下面的例子,f()函數包含一個對整型變量的引用。

1 void f(const int & i);
2 
3 int main()
4 {
5 f(2); /* OK */
6 }


    The program passes the R-Value 2 as an argument to f(). At runtime, C++ creates a temporary object of type int with the value 2 and binds it to the reference i. The temporary and its reference exist from the moment f() is invoked until it returns; they are destroyed immediately afterwards. Note that had we declared the reference i without the const qualifier, the function f() could have modified its argument, thereby causing undefined behavior. For this reason, you may only bind references to const objects.

    這段代碼將右值“2”做為函數f()的一個參數。代碼運行時,C++將創建一個值為2的臨時整型變量并將其與引用類型i綁定。這個臨時對象與它的接口將在 f()運行期間一直存到直到函數f返回。函數f返回后它們立即被釋放。注意我們沒有將i聲明為常量類型,但是函數f仍有可能修改它的這個參數,這將引起異常。因此最好是將引用與常量類型綁定。

    The same rule applies to user-defined objects. You may bind a reference to a temporary object only if it's const:

    同樣的規則適用于自定義類型。只有一個臨時對象為常量時才可以與引用類型綁定。

struct A{};

1 void f(const A& a);
2 
3 int main()
4 {
5     f(A()); /* OK, binding a temporary A to a const reference*/
6 }


    Tip 3: Comma-Separated Expressions

    話題3:逗號表達式

Comma-separated expressions were inherited from C. It's likely that you use such expressions in for- and while-loops rather often. Yet, the language rules in this regard are far from being intuitive. First, let's see what a comma separated expression is.

    逗號表達式是從C語言沿襲下來的。它就像你經常使用的for循環與while-loop循環一樣。但是這里面的語法規則遠不像看起來的那樣。首先,讓我們看看什么是逗號表達式。

    An expression may consist of one or more sub-expressions separated by commas. For example:

    一個表達式可以被逗號分隔為一個或若干個子表達式。例如:

1     if(++x, --y, cin.good()) /*three expressions*/


The if condition contains three expressions separated by commas. C++ ensures that each of the expressions is evaluated and its side effects take place. However, the value of an entire comma-separated expression is only the result of the rightmost expression. Therefore, the if condition above evaluates as true only if cin.good() returns true. Here's another example of a comma expression:

    這條if語句被逗號分事為三個表達式。從C++的角度每個表達式都是合法的但是副作用產生了。整個逗號表達式的值是由最右邊的表達式決定的。于是只有con.good()的返回值是true時整個表達式的值才是true。這里有另一個關于逗號表達式的例子。

1 int j=10;
2 int i=0;
3 
4 while++i, --j)
5 {
6     /*..repeat as long as j is not 0*/
7 }


    Tip 4: Calling a Function Before Program's Startup

    在程序啟動前調用函數

Certain applications need to invoke startup functions that run before the main program starts. For example, polling, billing, and logger functions must be invoked before the actual program begins. The easiest way to achieve this is by calling these functions from a constructor of a global object. Because global objects are conceptually constructed before the program's outset, these functions will run before main() starts. For example:

    有些應用需要在主程序啟運前運行啟動函數。例如投票、支付和登錄函數必須在實際種程序啟動前運行。一個最簡單的實現方法就是在一個全局對象的構造函數里調用這些函數。因為全局對象在程序的最開頭被隱式的創建,這些函數就可以在main()函數之前得到運行。例如:

 1 class Logger
 2 {
 3 public:
 4     Logger()
 5     {
 6         activate_log();
 7     }
 8 };
 9 
10 Logger log; /*global instance*/
11 
12 int main()
13 {
14     record * prec=read_log();
15     //.. application code
16 }


    The global object log is constructed before main() starts. During its construction, log invokes the function activate_log(). Thus, when main() starts, it can read data from the log file.

    全局對象log在main()函數啟動之前被創建。在它的構造函數里,log調用了active_log()函數。于是,當main()函數啟動時,它可以從日志文件中讀取數據。


posted on 2008-09-15 23:13 西門有悔 閱讀(2481) 評論(12)  編輯 收藏 引用

評論

# re: [原][譯]關于更好的編程風格的建議  回復  更多評論   

很好的東西呀!
Tip1里講的東西我以前還真沒意識到,一直用.h呢,呵呵。
2008-09-16 00:14 | abettor

# re: [原][譯]關于更好的編程風格的建議  回復  更多評論   

@abettor

謝謝。

我是第一次試著翻譯一些自己認為比較好的文檔,一方面是為了增加自己的知識,另一方面也是為與大家一起分享。

有什么翻譯得不好的地方歡迎討論。我的email:phoenix8848@gmail.com
2008-09-16 01:42 | 西門有悔

# re: [原][譯]關于更好的編程風格的建議  回復  更多評論   

As a rule, use “iostream” unless you're dealing with legacy code that is only compatible with “iostream.h”.
結論為除非在處理與“iostream”保持兼容的歷史遺留代碼時否則最好使用“iostream.h”。

這里翻譯有誤,應該譯為:應盡量使用“iostream”,除非你要處理的是只能與“iostream.h”兼容的遺留代碼。
2008-09-16 09:06 | autumnm1981

# re: [原][譯]關于更好的編程風格的建議  回復  更多評論   

const應該翻譯成常量
靜態是static
2008-09-16 10:02 | 新奧爾良土鱉

# re: [原][譯]關于更好的編程風格的建議  回復  更多評論   

為什么都喜歡留著英文?一跳一跳的,覺得不方便閱讀啊
2008-09-16 11:52 | 肥仔

# re: [原][譯]關于更好的編程風格的建議[未登錄]  回復  更多評論   

因為有時候翻譯可能不正確
2008-09-16 13:05 | 陳梓瀚(vczh)

# re: [原][譯]關于更好的編程風格的建議  回復  更多評論   

@autumnm1981

對啊。非常感謝!我馬上更新
2008-09-16 15:52 | 西門有悔

# re: [原][譯]關于更好的編程風格的建議  回復  更多評論   

@新奧爾良土鱉

寒啊。竟然犯這樣低級的錯誤。也反映了平時我以靜態變量與常量沒有在意。

謝謝。馬上更新。
2008-09-16 15:53 | 西門有悔

# re: [原][譯]關于更好的編程風格的建議  回復  更多評論   

@陳梓瀚(vczh)

對啊。昨天晚上沒有睡覺,半睡半醒狀態下翻的。說實話很多英文的文檔看起來還是很費勁。雖然俺也過了CET6,不過是五年前的事了。
2008-09-16 15:55 | 西門有悔

# re: [原][譯] Tips for Better Coding Style --- 關于更好的編程風格的建議[未登錄]  回復  更多評論   

睡覺是人生的終極目標……
2008-09-17 13:10 | 陳梓瀚(vczh)

# re: [原][譯] Tips for Better Coding Style --- 關于更好的編程風格的建議  回復  更多評論   

Thx to your great sharing!
From:Kven
2008-09-18 00:40 | Kven

# re: [原][譯] Tips for Better Coding Style --- 關于更好的編程風格的建議  回復  更多評論   

The same rule applies to user-defined objects. You may bind a reference to a temporary object only if it's const:

同樣的規則適用于自定義類型。只有一個臨時對象為常量時才可以與引用類型綁定。


這里的"only if it's const"感覺應該是修飾"reference",即翻譯為“只有當引用為const時才可以將其綁定到一個臨時對象”,《C++編程思想》上好像也提到了這點,不知道是不是我理解有誤。
2009-07-22 10:01 | k.j

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲美女av网站| 一区二区三区国产在线观看| 久久综合久久综合久久综合| 亚洲中字黄色| 亚洲乱码久久| 中文久久精品| 亚洲成人自拍视频| 国产女主播一区| 欧美日韩日韩| 欧美日韩精品福利| 欧美三级午夜理伦三级中视频| 欧美一区二区在线免费观看 | 亚洲自拍偷拍色片视频| 夜夜嗨一区二区| 亚洲无亚洲人成网站77777| 亚洲一区国产视频| 久久国产精品高清| 美女在线一区二区| 欧美午夜一区| 伊人久久综合97精品| 亚洲精选成人| 欧美在线91| 亚洲国产黄色片| 99re66热这里只有精品4| 午夜精品999| 欧美日韩成人一区二区| 国产亚洲精品一区二区| 91久久精品一区二区别| 性欧美暴力猛交另类hd| 欧美大尺度在线| 亚洲欧美国产77777| 欧美国产在线观看| 在线观看亚洲视频| 亚洲欧美激情视频| 欧美国产视频在线| 欧美一级免费视频| 国产精品久久精品日日| 亚洲理论在线| 欧美激情综合| 久久午夜激情| 亚洲第一精品久久忘忧草社区| 亚洲女同在线| 一本色道久久综合| 欧美日韩国产首页在线观看| 亚洲黄色一区| 亚洲人成7777| 国产精品无码永久免费888| 亚洲女爱视频在线| 亚洲综合色激情五月| 国产精品亚洲综合色区韩国| 亚洲资源在线观看| 亚洲夜晚福利在线观看| 国产日产欧产精品推荐色| 午夜一区二区三区在线观看| 亚洲一级二级| 影院欧美亚洲| 亚洲精品在线一区二区| 国产精品高清网站| 久久在线视频在线| 欧美v日韩v国产v| 亚洲天堂av在线免费观看| 亚洲一级片在线观看| 韩日视频一区| 99伊人成综合| 在线成人激情| 一区二区日本视频| 亚洲国产高清一区二区三区| 亚洲精品欧美专区| 国产综合色产| 一区二区三区日韩精品| 性做久久久久久久免费看| 狠狠色2019综合网| 亚洲欧美资源在线| 亚洲一区二区三区涩| 欧美福利电影在线观看| 免费成人在线视频网站| 国产视频在线观看一区| 亚洲午夜激情| 亚洲欧美日韩在线观看a三区 | 99国产成+人+综合+亚洲欧美| 羞羞答答国产精品www一本| 欧美一级久久久久久久大片| 欧美日韩p片| 亚洲乱码国产乱码精品精| 日韩亚洲欧美一区| 欧美国产精品专区| 亚洲乱码视频| 亚洲欧美一区二区在线观看| 欧美精品999| 亚洲少妇自拍| 久久久久91| 亚洲三级免费电影| 国产精品xxxav免费视频| 亚洲一区3d动漫同人无遮挡| 欧美一级免费视频| 亚洲激情网站| 国产欧美日本一区视频| 久久久久久有精品国产| 亚洲国产欧美不卡在线观看| 在线亚洲欧美| 亚洲第一二三四五区| 国产精品免费观看在线| 久久久免费精品视频| 亚洲精品一区二区三区四区高清| 午夜精品一区二区三区四区| 国产伊人精品| 蜜臀99久久精品久久久久久软件| 99国产精品久久久久老师| 国产日韩欧美黄色| 欧美婷婷六月丁香综合色| 久久亚洲精品中文字幕冲田杏梨| 亚洲午夜免费视频| 91久久精品国产91性色| 久久香蕉国产线看观看av| 亚洲在线成人精品| 中日韩高清电影网| 亚洲精品视频中文字幕| 亚洲国产一区二区三区高清| 国产精自产拍久久久久久| 欧美人成免费网站| 欧美成人免费网站| 欧美成人国产va精品日本一级| 久久成人资源| 久久精品一区中文字幕| 久久色在线观看| 欧美91视频| 欧美视频在线一区二区三区| 欧美少妇一区二区| 国产精品自在在线| 在线看一区二区| 亚洲国产精品成人精品| 久久久久久久久岛国免费| 久久久久久亚洲综合影院红桃 | 久久久国产精品一区二区三区| 国产一区二区三区奇米久涩| 亚洲一级二级在线| 在线观看国产成人av片| 欧美777四色影视在线| 欧美激情久久久久| 亚洲欧美视频一区二区三区| 国产欧美在线| 欧美日韩中文| 91久久精品国产91久久| 欧美激情二区三区| 亚洲国产综合91精品麻豆| 亚洲乱码国产乱码精品精可以看| 亚洲图色在线| 亚洲激情一区二区三区| 午夜在线一区| 欧美视频在线免费| 亚洲精品一品区二品区三品区| 午夜精品久久久久久久99热浪潮 | 久久久激情视频| 欧美成人一区在线| 一区二区三区www| 欧美一级二区| 国产精品久久久久久久午夜片| 国产一区二区三区不卡在线观看| 99re6热只有精品免费观看| 美女脱光内衣内裤视频久久网站| 亚洲五月六月| 国产三级欧美三级| 欧美在线视频一区二区三区| 一区二区三区四区五区精品视频| 欧美成人免费视频| 亚洲精品网站在线播放gif| 欧美国产亚洲视频| 欧美激情一区三区| 正在播放欧美视频| 亚洲图中文字幕| 国产精品欧美风情| 午夜久久电影网| 久久精品视频在线| 亚洲国产精品嫩草影院| 国产有码一区二区| 欧美成人小视频| 欧美午夜国产| 久久网站免费| 国产精品成av人在线视午夜片| 欧美一级艳片视频免费观看| 久久久久久久999精品视频| 在线亚洲高清视频| 国产精品影院在线观看| 欧美成人中文字幕| 欧美日韩一卡二卡| 欧美一区二区精品| 欧美网站在线观看| 久久欧美肥婆一二区| 欧美日韩亚洲国产一区| 欧美在线免费观看视频| 欧美日韩综合在线免费观看| 男人的天堂成人在线| 国产精品―色哟哟| 亚洲成人在线网| 国产欧美日韩免费看aⅴ视频| 亚洲大胆在线| 亚洲美洲欧洲综合国产一区| 欧美主播一区二区三区美女 久久精品人| 亚洲精品一区在线观看| 久久影视精品|