2013-03-25 5 views
12
class Foo 
{ 
    public: 
     const int x; 
}; 

class Bar 
{ 
    private: 
     const int x; 
}; 

Ausgang:`public` Zugangsqualifizierer und` const`ness. `-Wuninitialized`

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

Warum Bar eine Warnung erzeugen, aber Foo nicht (offensichtlich wegen der Zugang Qualifier, aber was ist die Logik?).

+0

Ist das das ganze Programm? –

+0

Dies ist die komplette MWE, die ich kompiliert habe. – aiao

Antwort

12

Mit diesen Definitionen, da Foo::x öffentlich ist, kann man wirksam eine Foo mit so etwas wie instanziiert: für eine Bar

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

oder

Foo f = { 0 }; 

Sie können nicht das tun.

+0

Das ist eine ungewöhnliche Syntax (zumindest für mich). Könnten Sie bitte eine Ressource bereitstellen? – aiao

+3

@aiao Die erste ist eine neue Syntax in C++ 11 (oft schlecht benannt [uniform initialization] (http://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization)) und entspricht der zweiten. Beide führen in diesem Fall eine Aggregat-Initialisierung durch (§ 8.5.1 von C++ 11). –

Verwandte Themen