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

平凡的世界

神鷹忽展翅,頭頂青天飛
隨筆 - 10, 文章 - 0, 評(píng)論 - 34, 引用 - 0
數(shù)據(jù)加載中……

[原][譯] Tips for Better Coding Style --- 關(guān)于更好的編程風(fēng)格的建議

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

關(guān)于更好的編程風(fēng)格的建議 (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.
    在這篇文章里我將談?wù)劯鞣N層次的C++程序員經(jīng)常問(wèn)及的四個(gè)問(wèn)題。例如我很驚訝地發(fā)現(xiàn)還有很多程序員沒(méi)有意識(shí)到標(biāo)準(zhǔn)頭文件擴(kuò)展名.h的爭(zhēng)議,命名空間的恰當(dāng)用法以及引用臨時(shí)對(duì)象的規(guī)則。這些問(wèn)題及其它將在這里進(jìn)行討論。

    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”頭文件名與現(xiàn)代、符合標(biāo)準(zhǔn)的“<XXX>”頭文件名記號(hào)之間的區(qū)別開(kāi)始。接下來(lái)我們探索C++不為人知的角落,由于編譯器的局限性和關(guān)聯(lián)語(yǔ)言規(guī)則某些隱蔽的自然特性迷惑了許多程序員,比如逗號(hào)分隔表達(dá)式的意義與引用型變量的規(guī)則。最后我們將學(xué)習(xí)如何在程序啟動(dòng)前啟動(dòng)一個(gè)函數(shù)。

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

    語(yǔ)題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”代替新的符合標(biāo)準(zhǔn)的“iostream”庫(kù)。兩者有什么區(qū)別呢?首先,標(biāo)準(zhǔn)頭文件“.h”擴(kuò)展名在五年前就倍受爭(zhēng)議。在新代碼中使用有爭(zhēng)議的(過(guò)時(shí)的)特性永遠(yuǎn)都不是一個(gè)好主意。從本質(zhì)上看,“iostream”包括一系列支持窄字符與寬字符的模板化(templatized) I/O輸入輸出類(lèi),而相反地,“iostream.h”只支持字符流。第三,iostream接口的標(biāo)準(zhǔn)C++規(guī)范在許多細(xì)節(jié)方面進(jìn)行了變動(dòng)。因此,“iostream”的接口與實(shí)現(xiàn)同那些“iostream.h”是有區(qū)別的。最后,“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”.

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

    Tip 2: Binding a Reference to an R-Value

    話(huà)題2:將引用與右值綁定

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

    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++編程的一個(gè)基本概念。本質(zhì)上來(lái)講右值是一個(gè)不可能出現(xiàn)在等號(hào)左邊的表達(dá)式。相反,左值引用一個(gè)對(duì)象(廣義范圍上的),或者一塊可讀寫(xiě)的內(nèi)存。引用既可以指向右值也可以指向左值。然而,由于語(yǔ)言在處理右值上的限制,你也得在將引用指向右值是慎重考慮。

    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應(yīng)為"常量",感謝新奧爾良土鱉 )一樣也是被允許的。這條原則背后的原理是很顯而易見(jiàn)的:你無(wú)法改變右值,因?yàn)閷?duì)常量的引用確保程序不會(huì)通過(guò)這個(gè)接口改變右值。下面的例子,f()函數(shù)包含一個(gè)對(duì)整型變量的引用。

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”做為函數(shù)f()的一個(gè)參數(shù)。代碼運(yùn)行時(shí),C++將創(chuàng)建一個(gè)值為2的臨時(shí)整型變量并將其與引用類(lèi)型i綁定。這個(gè)臨時(shí)對(duì)象與它的接口將在 f()運(yùn)行期間一直存到直到函數(shù)f返回。函數(shù)f返回后它們立即被釋放。注意我們沒(méi)有將i聲明為常量類(lèi)型,但是函數(shù)f仍有可能修改它的這個(gè)參數(shù),這將引起異常。因此最好是將引用與常量類(lèi)型綁定。

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

    同樣的規(guī)則適用于自定義類(lèi)型。只有一個(gè)臨時(shí)對(duì)象為常量時(shí)才可以與引用類(lèi)型綁定。

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

    話(huà)題3:逗號(hào)表達(dá)式

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.

    逗號(hào)表達(dá)式是從C語(yǔ)言沿襲下來(lái)的。它就像你經(jīng)常使用的for循環(huán)與while-loop循環(huán)一樣。但是這里面的語(yǔ)法規(guī)則遠(yuǎn)不像看起來(lái)的那樣。首先,讓我們看看什么是逗號(hào)表達(dá)式。

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

    一個(gè)表達(dá)式可以被逗號(hào)分隔為一個(gè)或若干個(gè)子表達(dá)式。例如:

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語(yǔ)句被逗號(hào)分事為三個(gè)表達(dá)式。從C++的角度每個(gè)表達(dá)式都是合法的但是副作用產(chǎn)生了。整個(gè)逗號(hào)表達(dá)式的值是由最右邊的表達(dá)式?jīng)Q定的。于是只有con.good()的返回值是true時(shí)整個(gè)表達(dá)式的值才是true。這里有另一個(gè)關(guān)于逗號(hào)表達(dá)式的例子。

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

    在程序啟動(dòng)前調(diào)用函數(shù)

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:

    有些應(yīng)用需要在主程序啟運(yùn)前運(yùn)行啟動(dòng)函數(shù)。例如投票、支付和登錄函數(shù)必須在實(shí)際種程序啟動(dòng)前運(yùn)行。一個(gè)最簡(jiǎn)單的實(shí)現(xiàn)方法就是在一個(gè)全局對(duì)象的構(gòu)造函數(shù)里調(diào)用這些函數(shù)。因?yàn)槿謱?duì)象在程序的最開(kāi)頭被隱式的創(chuàng)建,這些函數(shù)就可以在main()函數(shù)之前得到運(yùn)行。例如:

 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.

    全局對(duì)象log在main()函數(shù)啟動(dòng)之前被創(chuàng)建。在它的構(gòu)造函數(shù)里,log調(diào)用了active_log()函數(shù)。于是,當(dāng)main()函數(shù)啟動(dòng)時(shí),它可以從日志文件中讀取數(shù)據(jù)。


posted on 2008-09-15 23:13 西門(mén)有悔 閱讀(2471) 評(píng)論(12)  編輯 收藏 引用

評(píng)論

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

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

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

@abettor

謝謝。

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

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

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

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

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

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

const應(yīng)該翻譯成常量
靜態(tài)是static
2008-09-16 10:02 | 新奧爾良土鱉

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

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

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議[未登錄](méi)  回復(fù)  更多評(píng)論   

因?yàn)橛袝r(shí)候翻譯可能不正確
2008-09-16 13:05 | 陳梓瀚(vczh)

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

@autumnm1981

對(duì)啊。非常感謝!我馬上更新
2008-09-16 15:52 | 西門(mén)有悔

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

@新奧爾良土鱉

寒啊。竟然犯這樣低級(jí)的錯(cuò)誤。也反映了平時(shí)我以靜態(tài)變量與常量沒(méi)有在意。

謝謝。馬上更新。
2008-09-16 15:53 | 西門(mén)有悔

# re: [原][譯]關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

@陳梓瀚(vczh)

對(duì)啊。昨天晚上沒(méi)有睡覺(jué),半睡半醒狀態(tài)下翻的。說(shuō)實(shí)話(huà)很多英文的文檔看起來(lái)還是很費(fèi)勁。雖然俺也過(guò)了CET6,不過(guò)是五年前的事了。
2008-09-16 15:55 | 西門(mén)有悔

# re: [原][譯] Tips for Better Coding Style --- 關(guān)于更好的編程風(fēng)格的建議[未登錄](méi)  回復(fù)  更多評(píng)論   

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

# re: [原][譯] Tips for Better Coding Style --- 關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

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

# re: [原][譯] Tips for Better Coding Style --- 關(guān)于更好的編程風(fēng)格的建議  回復(fù)  更多評(píng)論   

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

同樣的規(guī)則適用于自定義類(lèi)型。只有一個(gè)臨時(shí)對(duì)象為常量時(shí)才可以與引用類(lèi)型綁定。


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

只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   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>
            亚洲看片网站| 亚洲大片免费看| 亚洲午夜激情在线| 国产欧美一区二区视频| 久久久精品一区二区三区| 久久综合一区二区| 91久久久亚洲精品| 亚洲国产精品999| 欧美精品v日韩精品v国产精品 | 亚洲一区亚洲| 极品尤物久久久av免费看| 女女同性精品视频| 欧美成人免费小视频| 亚洲一区二区三区视频| 欧美一级久久久久久久大片| 亚洲高清一二三区| 在线亚洲免费视频| 伊人成综合网伊人222| 亚洲精品乱码久久久久久久久| 国产精品一二| 欧美国产日本在线| 国产精品蜜臀在线观看| 欧美成人午夜剧场免费观看| 欧美日韩一级黄| 老鸭窝亚洲一区二区三区| 欧美日韩国产成人精品| 久久久久久久性| 欧美亚州一区二区三区| 欧美成人免费网站| 国产精品欧美经典| 亚洲黄色片网站| 伊人久久大香线| 午夜精品福利视频| 中文国产成人精品| 女人色偷偷aa久久天堂| 久久黄色网页| 欧美日韩一区在线观看视频| 欧美不卡视频一区| 国产一区二区三区精品欧美日韩一区二区三区 | 篠田优中文在线播放第一区| 亚洲精品欧美一区二区三区| 欧美一级专区| 香蕉久久精品日日躁夜夜躁| 欧美国产国产综合| 欧美电影电视剧在线观看| 国产网站欧美日韩免费精品在线观看| 亚洲精品视频在线播放| 亚洲精品1234| 美女主播一区| 欧美+亚洲+精品+三区| 国产亚洲欧美一区二区三区| 亚洲图片自拍偷拍| 亚洲图片在线观看| 欧美人与性动交α欧美精品济南到| 老司机成人网| 尤物在线精品| 久久精品夜夜夜夜久久| 久久视频一区| 在线观看国产日韩| 久久久www免费人成黑人精品| 欧美一区激情| 国产午夜亚洲精品不卡| 亚洲欧美春色| 久久久免费精品视频| 老色鬼精品视频在线观看播放| 欧美福利在线观看| 亚洲韩国一区二区三区| 99精品欧美一区二区蜜桃免费| 欧美激情一区二区| 亚洲青涩在线| 亚洲一区欧美| 国产欧美亚洲精品| 久久国产欧美日韩精品| 久久一二三四| 在线看日韩欧美| 欧美~级网站不卡| 日韩午夜激情av| 香蕉乱码成人久久天堂爱免费| 国产日韩高清一区二区三区在线| 欧美一区二区在线免费播放| 久久综合九色综合欧美就去吻 | 欧美亚州一区二区三区 | 久久精品五月| 欧美国产亚洲视频| 在线视频一区观看| 国产色综合网| 麻豆av一区二区三区久久| 亚洲电影自拍| 性色一区二区| 亚洲国产精品999| 欧美日韩中文| 久久久久久久久久码影片| 亚洲高清激情| 欧美制服丝袜第一页| 91久久中文字幕| 国产欧美日韩不卡| 免费看av成人| 欧美一区二区播放| 亚洲美女区一区| 老司机一区二区三区| 亚洲一区二区在线视频| 亚洲成色精品| 国产美女精品人人做人人爽| 蜜臀av国产精品久久久久| 亚洲一区欧美| 亚洲伦伦在线| 欧美黄色aaaa| 久久国产一区| 亚洲在线一区| 亚洲久久一区| 樱桃成人精品视频在线播放| 国产精品高潮在线| 欧美高清在线视频| 久久夜色精品国产欧美乱| 亚洲一区一卡| 一区二区三区久久| 亚洲激情国产| 欧美.com| 久久综合久久综合久久综合| 性色av一区二区怡红| 一本综合久久| 日韩视频二区| 亚洲精品小视频| 亚洲人成欧美中文字幕| 韩日精品在线| 国产在线不卡| 国产最新精品精品你懂的| 国产精品美女在线观看| 国产精品99免费看| 欧美视频在线一区| 欧美日韩日本视频| 欧美精品乱人伦久久久久久| 美女国产一区| 欧美激情亚洲| 一区二区三区视频观看| 亚洲日本成人在线观看| 亚洲第一在线视频| 亚洲国产精品精华液2区45| 国产视频亚洲精品| 国产在线欧美日韩| 国产视频精品网| 国产一区二区三区奇米久涩 | 久久精品一区二区三区不卡牛牛| 午夜精品久久久久久| 亚洲欧美日本伦理| 午夜综合激情| 久久aⅴ国产紧身牛仔裤| 性欧美暴力猛交69hd| 久久成人综合网| 卡一卡二国产精品| 欧美精品久久久久久久免费观看| 欧美高清在线视频| 国产精品电影观看| 国产亚洲一区二区在线观看 | 国内成+人亚洲| 在线观看欧美日韩| 亚洲精品乱码视频| 亚洲一区二区三区在线| 久久国产精品一区二区三区四区| 久久久久久久精| 亚洲第一色在线| 一区二区三区四区国产精品| 欧美一区二区视频网站| 老司机免费视频久久| 欧美乱在线观看| 国产伦精品一区二区三区照片91| 国产一区二区三区四区三区四 | 最新中文字幕亚洲| 亚洲一区精彩视频| 蜜臀av一级做a爰片久久 | 欧美激情黄色片| 一区二区三欧美| 欧美一级理论片| 欧美日韩hd| 黄色一区二区在线| 亚洲视频在线播放| 久久久最新网址| 99re8这里有精品热视频免费| 午夜精品三级视频福利| 毛片基地黄久久久久久天堂 | 久久这里有精品视频| 欧美日韩一区二区视频在线| 国产在线精品二区| 在线视频亚洲| 欧美国产日韩亚洲一区| 亚洲一区成人| 欧美日韩国产首页| 亚洲大片av| 久久精品99| 亚洲色图综合久久| 欧美成人在线影院| 激情另类综合| 久久av免费一区| 亚洲最新在线视频| 欧美精品免费观看二区| 亚洲高清电影| 免费不卡亚洲欧美| 欧美一区二区在线播放| 国产精品久久综合| 亚洲天堂激情|