문제

class Foo
{
    public:
        const int x;
};

class Bar
{
    private:
        const int x;
};

Output:

test.cpp:10:13: warning: non-static const member ‘const int Bar::x’ in class without a constructor [-Wuninitialized]

Why does Barproduce a warning but Foo doesn't (obviously because of access qualifier, but what is the logic?).

도움이 되었습니까?

해결책

With those definitions, since Foo::x is public, you can validly instantiate a Foo with something like:

Foo f { 0 }; // C++11

or

Foo f = { 0 };

You can't do that for a Bar.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top