Topic: Friendship inheritance


Author: "Ed Brey" <brey@afd.mke.etn.com>
Date: 1999/09/13
Raw View
Suppose a program is structured like this:

// File "1"
class A {
    int i;
    class B; friend B;
};

// File "2"
#include "1"
class A::B {
    int fn(A a) {return a.i};
};

Now say that an implementation detail in B changes so that file 2 looks like
this:

// File "2"
#include "1"
class A::B {
    int fn(A a) {C c; return c.fn();}
    class C {
        int fn(A a) {return a.i};
    };
    friend C;
};

This won't work because A::B::C can't access private data in A.  But should
this be the case?  The intention of A in granting B friendship is to allow B
full access to A, without regard for how B is implemented.  It shouldn't
matter whether B chooses to use nested classes or not.

Of course, B should get to control whether C has access to its private
members.  I contend that the private members of B should conceptually
include all members of A by virtue of B being a friend of A.  Therefore,
when B makes C a friend, C should now have access to private members of both
B and A.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]