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 何克勤 閱讀(199) 評論(0)  編輯  收藏 所屬分類: C/C++
          主站蜘蛛池模板: 枣庄市| 衡山县| 广水市| 焉耆| 紫阳县| 吴忠市| 林州市| 天祝| 富锦市| 茂名市| 米易县| 科技| 太原市| 赣榆县| 郎溪县| 苏尼特右旗| 穆棱市| 万宁市| 万州区| 衡东县| 井冈山市| 德清县| 新营市| 昭苏县| 云阳县| 河北区| 陆河县| 河东区| 水城县| 哈尔滨市| 泸西县| 静安区| 保靖县| 河东区| 金坛市| 黄山市| 宁安市| 新余市| 上饶县| 南平市| 信丰县|