// 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 呢?
这个值是怎么来的阿?
// 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 呢?
这个值是怎么来的阿?
因为赋123的amount是全局变量,赋456的是局部变量,这两个同名变量储存在不同的内存区域,有不同的生存周期。就像不同城市里的两个同名同姓的人一样。进入到main之后再说amount是局部变量了,不再是前面初始化过的全局变量amount了。
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
}
如果是上面的话,应该是楼上说的那样.
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
}
因为单步调试的时候,光标停在
int amount = 456;
这一行的时候,实际上只执行了局部变量amount的声明,要在往下执行一步(f10)只后,才会把456赋给amount。其实编译之后执行的是汇编语句,
int amount;
amount = 456;
和
int amount = 456;
对于机器来说是没有区别的。