<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 134,comments - 22,trackbacks - 0
    2.3.5. Declarations and Definitions

    As we'll see in Section 2.9 (p. 67), C++ programs typically are composed of many files. In order for multiple files to access the same variable, C++ distinguishes between declarations and definitions.

    就像我們在2.9 (p. 67)節看到的一樣,典型的C++程序通常會由好多文件組成。為了使不同的文件都可以訪問同一個變量,C++會區分變量的聲明(declarations)和定義(definitions)。

    A definition of a variable allocates storage for the variable and may also specify an initial value for the variable. There must be one and only one definition of a variable in a program.

    變量的定義(definitions)會為這個變量分配存儲空間,并且可能會為其指定一個初始化的值。在程序里,一個變量必須有一個,也只能有一處定義(definitions)。

    A declaration makes known the type and name of the variable to the program. A definition is also a declaration: When we define a variable, we declare its name and type. We can declare a name without defining it by using the extern keyword. A declaration that is not also a definition consists of the object's name and its type preceded by the keyword extern:

    變量的聲明(declarations)會將變量的類型和名稱傳達給程序。當然,定義(definitions)也是一種聲明:當我們定義一個變量的時候,我們當然也聲明了他的名稱和類型。我們可以通過使用“extern”關鍵字來聲明(declarations)一個變量,而不用定義(definitions)它。聲明(declarations)的形式就是在對象(變量)的名字和類型前面,加上關鍵字“extern:

          extern int i;   // declares but does not define i

          int i;          // declares and defines i

    An extern declaration is not a definition and does not allocate storage. In effect, it claims that a definition of the variable exists elsewhere in the program. A variable can be declared multiple times in a program, but it must be defined only once.

    帶“extern”關鍵字的語句屬于聲明(declarations),不是定義(definitions),他不會給變量分配內存。實際上,它是說明程序中的某處包含這個變量的定義。一個變量可以在程序中被聲明(declarations)多次,但是只能被定義(definitions)一次。

    A declaration may have an initializer only if it is also a definition because only a definition allocates storage. The initializer must have storage to initialize. If an initializer is present, the declaration is treated as a definition even if the declaration is labeled extern:

    聲明(declarations)時你可以給變量初始化。但是一旦你這樣做,那么這句話也就變成了定義(definitions),因為只有在定義(definitions)的時候才會為變量分配內存。初始化的時候必然要為初始值分配存儲空間。如果你在聲明(declarations)的時候同時初始化了變量,即便“extern”關鍵字存在,這個語句也會認為是定義(definitions)。

          extern double pi = 3.1416; // definition

    Despite the use of extern, this statement defines pi. Storage is allocated and initialized. An extern declaration may include an initializer only if it appears outside a function.

    不管有沒有“extern”關鍵字存在,這條語句的作用也是定義(definitions)“pi”。變量已經被分配了內存,并且賦予了初始值。聲明(declarations)只有在一種情況下可以被初始化,那就是當他被放置在函數外部的時候。

    Because an extern that is initialized is treated as a definition, any subseqent definition of that variable is an error:

    由于包含初始化的聲明(declarations)語句會被認為是定義(definitions),所以如下的用法會被認為是錯誤的:

          extern double pi = 3.1416; // definition

          double pi;                 // error: redefinition of pi

    Similarly, a subsequent extern declaration that has an initializer is also an error:

    同樣的,定義(definitions)后再使用同樣的聲明(declarations)也是錯誤的:

          extern double pi = 3.1416; // definition

          extern double pi;          // ok: declaration not definition

          extern double pi = 3.1416; // error: redefinition of pi

    The distinction between a declaration and a definition may seem pedantic but in fact is quite important.

    聲明(declarations)和定義(definitions)之間的區別看似有些賣弄學問的嫌疑,但是其實是非常重要的。

    Note

    In C++ a variable must be defined exactly once and must be defined or declared before it is used.

    筆記

    C++里,變量必須被定義一次,最多一次,至少一次,而且必須在使用前定義(definitions)或者聲明(declarations)。

    Any variable that is used in more than one file requires declarations that are separate from the variable's definition. In such cases, one file will contain the definition for the variable. Other files that use that same variable will contain declarations for but not a definition of that same variable.

    任何一個在多個文件中都要用到的變量都需要在沒有其定義(definitions)的文件里對其進行聲明(declarations)。在這樣的情況下,一般一個文件會包含該變量的定義(definitions)。其他用到該變量的文件就會包含該變量的聲明(declarations),而不是定義(definitions)。

    Exercises Section 2.3.5

    Exercise 2.18:

    Explain the meaning of each of these instances of name:

          extern std::string name;

          std::string name("exercise 3.5a");

          extern std::string name("exercise 3.5a");

    posted on 2009-11-27 14:10 何克勤 閱讀(196) 評論(0)  編輯  收藏 所屬分類: C/C++
    主站蜘蛛池模板: 成人亚洲性情网站WWW在线观看| 亚洲视频在线观看不卡| 中文字幕高清免费不卡视频| 亚洲AV午夜成人片| 日韩av无码成人无码免费| 国产区图片区小说区亚洲区| 色噜噜AV亚洲色一区二区| 在线看片v免费观看视频777| 国产亚洲情侣久久精品| 亚洲av网址在线观看| 在线观看无码的免费网站| 人妻免费一区二区三区最新| 亚洲精品无码你懂的| 亚洲国产精品成人精品无码区在线| 青青青国产在线观看免费网站| sihu国产精品永久免费| 亚洲影视自拍揄拍愉拍| 国产亚洲精品线观看动态图| 免免费国产AAAAA片| 一级特黄录像视频免费| 男人天堂2018亚洲男人天堂| 亚洲香蕉成人AV网站在线观看| 97在线观免费视频观看| a级大片免费观看| 美女羞羞免费视频网站| 亚洲三级中文字幕| 国产亚洲精品无码成人| 国产在线a不卡免费视频| 99无码人妻一区二区三区免费| a级毛片毛片免费观看久潮喷| 亚洲丶国产丶欧美一区二区三区| 亚洲AV日韩AV永久无码绿巨人| 亚洲免费日韩无码系列| 好男人看视频免费2019中文| 蜜桃成人无码区免费视频网站| 一级做a免费视频观看网站| 亚洲影院天堂中文av色| 亚洲综合免费视频| 亚洲成AV人片一区二区密柚| 亚洲午夜国产片在线观看| 日韩免费一级毛片|