出于好奇,我尝试了这个代码, 由访谈提问产生[*]
int main(int argc, char *argv[])
{
int a = 1234;
printf("Outer: %d
", a);
{
int a(a);
printf("Inner: %d
", a);
}
}
在Linux(g+4.6.3和clogg+3.0)上汇编时,输出:
Outer: 1234
Inner: -1217375632
但在Windows(VS2010)上,
Outer: 1234
Inner: 1234
其理由是,在第二个变量的复制构件完成之前,第一个变量仍然可以访问。 但我不确定这是标准行为还是微软(其他)奇格。
任何想法吗?
[*] 实际问题是:
您如何在不使用临时变量或全球变量的情况下在包含范围中具有相同名称变量值的变量范围内将变量调出?
{
// Not at global scope here
int a = 1234;
{
int a;
// how do you set this a to the value of the containing scope a ?
}
}