// expre_ScopeResolutionOperator.cpp
// compile with: /EHsc
// Demonstrate scope resolution operator
#include <iostream>using namespace std;int amount = 123;             // A global variablevoid main() {
   int amount = 456;          // A local variable
   cout  <<::amount << endl  // Print the global variable
         << amount << endl;   // Print the local variable
   
}
单步执行到 main() 和 int amount = 456; 之间,amount值为社么是-858993460 呢?
这个值是怎么来的阿?

解决方案 »

  1.   

    呵呵~~DEBUG版似乎是默认的值,RELEASE版似乎是不确定的值。不过楼主关心的估计是,前面已经把amount赋了123了,怎么还说没初始化吧?
    因为赋123的amount是全局变量,赋456的是局部变量,这两个同名变量储存在不同的内存区域,有不同的生存周期。就像不同城市里的两个同名同姓的人一样。进入到main之后再说amount是局部变量了,不再是前面初始化过的全局变量amount了。
      

  2.   

    不应该吧
    int amount = 123;             // A global variablevoid main() {
       int amount ;/////////////
        amount  = 456;          // A local variable
       cout  <<::amount << endl  // Print the global variable
             << amount << endl;   // Print the local variable
       
    }
    如果是下面的,不应该把,local variable 的值就应该是456;它在定义的时候就已经给初期值了啊.
    int amount = 123;             // A global variablevoid main() {
       int amount = 456;          // A local variable
       cout  <<::amount << endl  // Print the global variable
             << amount << endl;   // Print the local variable
       
    }
    如果是上面的话,应该是楼上说的那样.
      

  3.   

    不应该吧
    int amount = 123;             // A global variablevoid main() {
       int amount ;/////////////
        amount  = 456;          // A local variable
       cout  <<::amount << endl  // Print the global variable
             << amount << endl;   // Print the local variable
       
    }
    如果是上面的话,应该是楼上说的那样.
    如果是下面的,不应该把,local variable 的值就应该是456;它在定义的时候就已经给初期值了啊.
    int amount = 123;             // A global variablevoid main() {
       int amount = 456;          // A local variable
       cout  <<::amount << endl  // Print the global variable
             << amount << endl;   // Print the local variable
       
    }
      

  4.   

    你用的肯定是Debug版本,编译器会加一段程序进去,将所有的变量初始化成0xcc,-858993460就是0xcccccccc嘛。
      

  5.   

    呵呵~~追印误会了
    因为单步调试的时候,光标停在
    int amount = 456;
    这一行的时候,实际上只执行了局部变量amount的声明,要在往下执行一步(f10)只后,才会把456赋给amount。其实编译之后执行的是汇编语句,
    int amount;
    amount = 456;

    int amount = 456;
    对于机器来说是没有区别的。