"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
Both gcc 3.3.1 and VC++ 7.1 compile the following code.
struct Outer
{
struct Inner
{
int f() { return c; }
};
private:
static const int c;
};
const int Outer::c = 123;
int main()
{
Outer::Inner o;
std::cout << o.f() << '\n';
}
Obviously I'm wrong but I thought the above breaks two different rules of
C++. Firstly isn't c private, secondly how can Inner access a member of
Outer without qualifying the name (i.e. return Outer::c)?
Explanations and references to the standard would be appreciated.
Am perplexed about your 1st question.
But as for your second question -
Section 9.7/1
"Except by using explicit pointers, references, and object names, declarations
in a nested class can use only type names, static members, and enumerators from
the enclosing class". [Example:
int x;
int y;
class enclose {
public:
int x;
static int s;
class inner {
void f(int i)
{
int a = sizeof(x); // error: refers to enclose::x
x = i; // error: assign to enclose::x
s = i; // OK: assign to enclose::s
::x = i; // OK: assign to global x
y = i; // OK: assign to global y
}
void g(enclose* p, int i)
{
p->x = i; // OK: assign to enclose::x
}
};
};