相关疑难解决方法(0)

在C++中,阴影变量名称的范围分辨率("优先顺序")是多少?

在C++中,阴影变量名称的范围分辨率("优先顺序")是多少?我似乎无法在网上找到简明的答案.

例如:

#include <iostream>

int shadowed = 1;

struct Foo
{
    Foo() : shadowed(2) {}

    void bar(int shadowed = 3)
    {
        std::cout << shadowed << std::endl;
            // What does this output?

        {
            int shadowed = 4;
            std::cout << shadowed << std::endl;
                // What does this output?
        }
    }

    int shadowed;
};


int main()
{
    Foo().bar();
}
Run Code Online (Sandbox Code Playgroud)

我想不出变量可能会发生冲突的任何其他范围.如果我错过了,请告诉我.

shadowbar成员函数内部时,所有四个变量的优先级顺序是多少?

c++ variables scope scope-resolution shadowing

16
推荐指数
2
解决办法
2611
查看次数

标签 统计

c++ ×1

scope ×1

scope-resolution ×1

shadowing ×1

variables ×1