chapter2 变量 2.1 基本的寻址和变量声明

http://grass-and-moon.cnblogs.com 

from http://www.learncpp.com/cpp-tutorial/21-basic-addressing-and-variable-declaration/

在前面关于变量的课程中,我们已经了解了变量时能够存储一定信息的内存片段的命名。简言之,计算机具有随机存取存储器能够被程序中的变量使用。当变量被声明的时候,一片内存段分配给变量。

内存的最小单元是用二进制数表示的,比特,能够存取0和1.你可以把一个比特考虑成传统的灯的开关——当灯开的时候为1,关的时候为0.除了0和1没有别的选择。如果你查看一段内存,你能看到的仅仅是…011010100101010…类似的内容。内存被组织成独立的部分,称作地址。出乎意料的,在现代计算机中,每一bit并没有它自己的地址,最小的地址单元是有8bit的内存组成的,叫做byte,字节。

下面的图显示了一个序列的地址,和相应的数据的字节。

因为所有的数据在计算机中都是一个序列的位,所以我们使用数据类型告诉我们如何将内存中的内容进行解释。你已经看到了数据类型的一个例子:整型。当我们将一个变量声明为整型时,我们告诉计算机“这个变量地址下的内存片段将被解释成一个整型变量。”

当你将一个值赋值给一个数据类型时,计算机会考虑到编码的细节,将你的值编码成适合这个数据类型的位序列。当你取回你的值时,程序通过内存中这个序列重构你的数值。

 

声明一个变量

在基本C++一节中,我们学习了如何声明一个整型变量:

   1: int nVarName; // int is the type, nVarName is the name of the variable

 

将变量声明为其他的类型,基本的想法是一样的。

在下面的例子中,我们声明了5中变量。

   1: bool bValue;
   2: char chValue;
   3: int nValue;
   4: float fValue;
   5: double dValue;

在声明的时候,你可以进行赋值。当我们使用赋值符号进行赋值时,叫做explicit assignment。

   1: int nValue = 5; // explicit assignment

 

当然你也可以采用另一种赋值方式,implicit assignment

   1: int nValue(5); // implicit assignment

 

尽管implicit assignment看上去像是函数的调用,编译器依然能够很好的分别什么是变量,什么是函数。

 

多变量声明

用逗号间隔

   1: int nValue1, nValue2;

 

   1: int nValue1;
   2: int nValue2;

也能如下声明

   1: int nValue1 = 5, nValue2 = 6;
   2: int nValue3(7), nValue4(8);

 

这和采用下面的方式相同

   1: int nValue1 = 5;
   2: int nValue2 = 6;
   3: int nValue3 = 7;
   4: int nValue4 = 8;

 

新手容易犯的3个错误。

1.

   1: int nValue1, int nValue2; // wrong (compiler error)
   2:  
   3: int nValue1, nValue2; // correct

 

2.

   1: int nValue1, double dValue2; // wrong (compiler error)
   2:  
   3: int nValue1; double dValue2; // correct (but not recommended)
   4:  
   5: // correct and recommended (easier to read)
   6: int nValue1;
   7: double dValue2;

 

3.

   1: int nValue1, nValue2 = 5; // wrong (nValue1 is uninitialized!)
   2:  
   3: int nValue1 = 5, nValue2 = 5; // correct

 

 

声明变量的位置

c编译器强制用户在函数的开始声明所有的变量。

   1: int main()
   2: {
   3:     // all variable up top
   4:     int x;
   5:     int y;
   6:  
   7:     // then code
   8:     using namespace std;
   9:     cout << "Enter a number: ";
  10:     cin >> x;
  11:  
  12:     cout << "Enter another number: ";
  13:     cin >> y;
  14:  
  15:     cout << "The sum is: " << x + y << endl;
  16:     return 0;
  17: }

 

这种风格已经被废止了。C++编译器并没有建议将所有的变量声明在函数的开头。更合适的C++风格是哪里需要使用它就在哪里声明。

   1: int main()
   2: {
   3:     // then code
   4:     using namespace std;
   5:  
   6:     cout << "Enter a number: ";
   7:     int x; // we need x starting here.
   8:     cin >> x;
   9:  
  10:     cout << "Enter another number: ";
  11:     int y; // we don't need y until now
  12:     cin >> y;
  13:  
  14:     cout << "The sum is: " << x + y << endl;
  15:     return 0;
  16: }

 

这样能够带来不少的好处。首先,变量在需要时才进行声明。如果x生命在函数的开头,我们只有找到x的位置才能够知道x被用来干什么。将x声明在input/output语句附近有助于一目了然知道这个变量是被用来输入或是输出的。

其次,将变量声明在需要的地方,告诉我们这个变量不会影响上面的部分,使我们的代码更容易理解和阅读。

最后,减少了不小心没有最变量进行初始化的可能,因为我们在声明后马上就对它进行初始化。

规则:在需要的地方声明变量。

详细见原文。

posted @ 2012-05-01 00:01  grassofsky  阅读(295)  评论(0编辑  收藏  举报