c++ - Memberspaces may access private members of parent class -
i've been reading this article, , playing around memberspace idiom while when noticed surprised me within snippet (which compiles without problems: http://ideone.com/hriv5b):
class hugeclass { public: struct memberspace { int f() const { return parent.f; } private: friend hugeclass; explicit memberspace(hugeclass & parent) : parent(parent) {} hugeclass & parent; } memberspace; hugeclass() : memberspace(*this), f(42) {} private: int f; };
i have expected compiler error access of hugeclass::f
not allowed because f
private in context.
hugeclass
friend
of memberspace
, hugeclass
may call private constructor of memberspace
, why work other way around without explicitly declaring memberspace
friend
of hugeclass
?
by language rules in c++11.
nested class member , such has same access rights other member. example:
class e { int x; class b { }; class { b b; // ok: e::i can access e::b void f(e* p, int i) { p->x = i; // ok: e::i can access e::x } }; };
and in c++03 was
members of nested class have no special access members of enclosing class, nor classes or functions have granted friendship enclosing class; usual access rules (clause 11) shall obeyed.
so, example c++11 should not work c++03 compilers.
Comments
Post a Comment