class A
{
int field;
class B
{
void f()
{
A a;
a.field;
}
};
};
Почему это компиляется?
Не слышал, чтоб вложенный класс также имел возможность доступа к приватным полям "класса-контейнера".
Здравствуйте, johny5, Вы писали:
J>Почему это компиляется?
J>Не слышал, чтоб вложенный класс также имел возможность доступа к приватным полям "класса-контейнера".
Ваш компилятор ошибается. Подобный код не должен компилироваться.
Clause 11.8 (Nested classes) verse 1:
The members of a nested class have no special access to members of an enclosing class, nor to classes or
functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be
obeyed. The members of an enclosing class have no special access to members of a nested class; the usual
access rules (clause 11) shall be obeyed. [Example:
class E {
int x;
class B { };
class I {
B b; // error: E::B is private
int y;
void f(E* p, int i)
{
p->x = i; // error: E::x is private
}
};
int g(I* p)
{
return p->y; // error: I::y is private
}
};
—end example]
Здравствуйте, Greg Zubankov, Вы писали:
GZ>Здравствуйте, johny5, Вы писали:
J>>Почему это компиляется?
J>>Не слышал, чтоб вложенный класс также имел возможность доступа к приватным полям "класса-контейнера".
GZ>Ваш компилятор ошибается. Подобный код не должен компилироваться.
GZ>Clause 11.8 (Nested classes) verse 1:
GZ>GZ>The members of a nested class have no special access to members of an enclosing class, nor to classes or
GZ>functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be
GZ>obeyed. The members of an enclosing class have no special access to members of a nested class; the usual
GZ>access rules (clause 11) shall be obeyed. [Example:
GZ>
GZ>class E {
GZ> int x;
GZ> class B { };
GZ> class I {
GZ> B b; // error: E::B is private
GZ> int y;
GZ> void f(E* p, int i)
GZ> {
GZ> p->x = i; // error: E::x is private
GZ> }
GZ> };
GZ>
int g(I* p)
GZ> {
GZ>
return p->y;
// error: I::y is private //**** line 7
GZ> }
GZ>};
GZ>
GZ>—end example]
Comeau, MSVC 7.1 SP1 ругаются только на
"ComeauTest.c", line 16: error: member "E::I::y" (declared at line 7) is inaccessible
return p->y; // error: I::y is private
msvc.cpp(16) : error C2248: 'E::I::y' : cannot access private member declared in class 'E::I'
msvc.cpp(7) : see declaration of 'E::I::y'
msvc.cpp(5) : see declaration of 'E::I'
За пункт стандарта — спасибо. Заглянул у себя, вот что нашёл (код подправил, лишняя скобочка была):
11.8 Nested classes [class.access.nest]
1 A nested class is a member and as such has the same access rights as any other member. The members of an enclosing
class have no special access to members of a nested class; the usual access rules (clause 11) shall be obeyed. [ Example:
class E {
int x;
class B { };
class I {
B b; // OK: E::I can access E::B
int y;
void f(E* p , int i)
{
p->x = i; // OK: E::I can access E::x
}
};
int g(I* p)
{
return p->y; // error: I::y is private
}
};
Доставайте

, у мя драфт от 2005-10-19, а у вас?
Здравствуйте, johny5, Вы писали:
J>Доставайте
, у мя драфт от 2005-10-19, а у вас?
ISO/IEC 14882:2003(E)