需要在其他部分访问带有值5的变量x

Need to access variable x with value 5 in else portion

本文关键字:变量 其他部 访问      更新时间:2023-10-16
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。