访问嵌套块中重新定义的变量
Access to redefined variables inside a nested block
请考虑以下程序:
#include <iostream>
using std::cout;
int i = 10;
int main() {
int i = 9;
{
int i = 8;
{
int i = 7;
cout << i << "n"; // i = 7
cout << ::i << "n"; // i = 10 (global i)
// How could we access to other i declarations here?
}
}
return 0;
}
在嵌套块中使用i
引用i
的最后一个声明,因为后者隐藏了其他声明。为了使用全局i
(函数 main 之外的声明(,我们可以使用 ::i
我想知道是否有办法在嵌套块中使用i
的其他十折
声明一个新的变量名称,类似于"j"而不是"i"。
#include <iostream>
using std::cout;
int i = 10;
int main() {
int i = 9;
{
int j = 8;
{
int j = 7;
cout << j << "n"; // j = 7
cout << ::i << "n"; // i = 10 (global i)
// How could we access to other
i declarations here?
}
}
return 0;
}
相关文章:
- 在头文件中定义变量不会出错
- 如何使用 SFINAE 在方法调用中有条件地定义变量?
- 在命名空间中定义变量
- 是否可以在C++中基于程序集输出(.dll或.exe)定义变量
- 用不同类型重新定义变量
- 具有全局定义变量的递归比没有全局定义变量的递归函数获得更多的堆栈.为什么?(跳入C++)
- 定义变量类型
- 在命名空间中定义变量,但在测试中获取空值
- cmake没有定义变量
- C 中的继承:在亲子类中定义变量
- 在同一函数中定义变量及其静态等效项
- 如何在 Tensorflow C++ 中定义变量的自定义有状态 Op 保存值
- 如何将 int 和 int* 传递到函数中以定义变量
- 在定义变量时调用类函数
- 定义变量与模板
- 定义变量类型
- 为什么您可以在 switch 语句中的"默认"下定义变量,而不能在"case"下定义变量
- C 如何基于另一个变量和某些算术定义变量的值
- C 可以指向具有定义变量的函数
- C 定义变量设置值