On 10 Mai, 14:14, Pavel Minaev <int...@[EMAIL PROTECTED]
> wrote:
> On 10 ???, 07:26, sat_a...@[EMAIL PROTECTED]
wrote:
>
>
>
> > In the following code, nested class B is accessing private member of
> > A. Is this allowed? This code compiles on Comeau online test compiler
> > and Visual Studio 2005.
>
> > class A
>
> > {
> > class B
> > {
> > A* a;
> > int bVal;
> > B() { bVal = a->aVal; } // This compiles
> > };
>
> > private:
>
> > int aVal;
>
> > };
>
> No, this is not allowed. I can't say anything about Comeau, but it is
> a well-known extension in Visual C++. To be standard-compliant, you
> need an explicit friend declaration for the innter class in the outer
> class.
This statement is probably too strict. Let's say, that the
current standard uses a wording, which can be interpreted
differently. There exists an arguments, which says that the
standard already allows this by combining 11/p.1
"A member of a class can be
— private; that is, its name can be used only by members and friends
of the class in which it is declared.[..]"
with [class.access.nest]/p.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
friend****p to an enclosing class; the usual access rules (clause 11)
shall be obeyed.[..]"
Note also that your assignment of friend****p to an inner class
has also a wording problem, because according to [class.friend]/p.1:
"A friend of a class is a function or class that is not a member of
the class but is permitted to use the private and protected member
names from the class. The name of a friend is not in the scope of
the class, and the friend is not called with the member access
operators (5.2.5) unless it is a member of another class.[..]"
which can be interpreted to mean that I cannot assign friend****p
to an inner class, see
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html##77
Greetings from Bremen,
Daniel Krügler
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|