需要在其他部分访问带有值5的变量x
Need to access variable x with value 5 in else portion
int main()
{
int x = 5;
if(0) {
}
else if(int x = 0) {
}
else {
cout << "I want to access variable x with value 5";
}
}
此处定义的变量x也将在其他块中可用,但是如果我需要访问MAIN中定义的变量,则如何进行操作。
不可能,内部x隐藏了外部。我认为编译器将至少使用pedantic
警告您,并取决于Comipler。
相关文章:
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 类与私有变量的其他类之间的线程安全性
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 是否可以使用其他变量为变量分配值,而无需在 C++ 中更改其值?
- 如何从其他结构访问受保护的结构变量
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- 为什么当我没有变量或其他任何命名的东西时"no match for 'operator==' "错误?
- 访问从 CPP 文件到其他头文件的静态变量
- 将其他函数中的变量用于主函数C++
- 如何将几个变量复制到其他变量
- 继承构造函数和其他变量的解决方法
- C++ 在方程中使用变量;错误:表达式必须具有整数或无作用域枚举类型及其他
- 变量 z 无法从其他函数访问变量 x
- 没有模板变量的模板化类型和其他使用此类的模板化类型
- 从其他类声明类类型变量会给出 C2143 错误(在"*"之前缺少";")
- 我的 c 字符串复制函数正在损坏其他变量的堆栈
- 如何在需要提及需要循环声明的其他类的类中初始化变量?
- 使用全局变量初始化不同编译单元中的其他全局变量
- C++setenv 解析其他变量
- 仅为特定条件重置变量的值,并为其他所有条件重置固定值