In article <4_***************@newssvr14.news.prodigy.com>, Mark P
<no*@my.real.email> writes
Is this legal and sensible?
class Outer
{
friend struct Inner
{
...
};
...
};
Basically I want to define the struct Outer::Inner and simultaneously
make it a friend to give it access to private details of Outer. My
compiler (gcc) accepts this, but then it also gives Inner private
access to Outer always so I can't tell if it does what I want it to do.
Thanks,
Mark
There are two distinct answers here.
1) The Standard as written pulled a major boo-boo because the actual
wording does not allow granting of friendship inwards only outwards or
sideways.
class x;
class y {
friend class x; // OK
class z;
friend class z; // technical error
class a {
friend class z; // OK
// ...
};
The problem is that as written the Standard also prohibits inner classes
having access to an outer class' non-public interfaces.
This problem has now been fixed (but I cannot remember if that fix has
been published in a TC yet. It has been resolved in favour of granting
inner classes unconditional access to an enclosing class' members. That
means that the technical error above will remain an error but it will
not be necessary to write such a statement.
2) All current compilers allow the technical error above and I do not
know of any that even give a diagnostic for it. I suspect that it will
remain acceptable for the foreseeable future except when compilers are
acting in the strictest conforming mode. If you want your code to be
portable, continue to write such declarations, though increasingly they
will be unnecessary as compilers adopt the new requirement (that a
nested class be a full member of its enclosing class with full access
privileges.)
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see
http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects