Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`public` access qualifier and `const`ness. `-Wuninitialized`

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?).

like image 996
aiao Avatar asked Mar 25 '13 10:03

aiao


1 Answers

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.

like image 88
Mat Avatar answered Oct 20 '22 00:10

Mat