c++ - Nested enum and nested class have different behavior -
consider these 2 examples:
struct x { class e { static const int z = 16 }; static const int b = x::z; // x has no member z }; struct y { enum e { z = 16 }; static const int b = y::z; // ok };
is there section of standard explains behavior?
yes, there such sections in c++ standard.
the first 1 is
9.9 nested type names
1 type names obey same scope rules other names. in particular, type names defined within class definition cannot used outside class without qualification.
it more precisely cite following quote
2 name of class member shall used follows: — in scope of class (as described above) or class derived (clause 10) class, — after . operator applied expression of type of class (5.2.5) or class derived class, — after -> operator applied pointer object of class (5.2.5) or class derived class, — after :: scope resolution operator (5.1) applied name of class or class derived class.
and second 1 is
11.7 nested classes
1 nested class member , such has same access rights other member. members of enclosing class have no special access members of nested class; the usual access rules (clause 11) shall obeyed.
in definition if not take account typo (the absence of semicolon after definition of z)
struct x { class e { static const int z = 16 }; static const int b = x::z; // x has no member z };
you trying access z 1) without qualification , 2) has private access control.
the correct definition as
struct x { class e { public: static const int z = 16; }; static const int b = e::z; };
as enumeration enumerators of unscopped enumeration members of class enumeration defined.
9.2 class members
1 member-specification in class definition declares full set of members of class; no member can added elsewhere. members of class data members, member functions (9.3), nested types, , enumerators.
Comments
Post a Comment