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

aiao
  • 4,621
  • 3
  • 25
  • 47

1 Answers1

12

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • That some unusual syntax (for me at least). Could you please provide a resource. – aiao Mar 25 '13 at 10:45
  • 3
    @aiao The first is new syntax in C++11 (often badly named [uniform initialization](http://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization)) and is equivalent to the second. They both, in this case, perform aggregate initialization (§8.5.1 of C++11). – Joseph Mansfield Mar 25 '13 at 10:51