Hicham Mouline
11/14/2008 3:38:00 PM
The suggestion would be to have protected or private friendship, depending
on "where"
in the class def the friend declaration was:
1)
class A {
protected:
friend void Base::f() const;
};
then Base::f() and all its overriding derived are friends,
2)
class A {
private:
friend void Base::f() const;
};
then just Base::f() is friend,
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:gfk54k$rce$1@news.datemas.de...
> Hicham Mouline wrote:
>> Hello,
>> A semi-skeptical colleague is asking me why friendship is not
>> inheritable, specifically:
>>
>> class Base {
>> public:
>> virtual void f() const =0;
>> };
>>
>> class Derived : public Base {
>> virtual void f() const { // impl };
>> };
>>
>>
>> class A {
>> friend void Base::f() const;
>> };
>>
>>
>> In the implementation of Derived::f() const,
>> it cannot access private members of A.
>> you have to actually make Derived::f() friend of A.
>>
>> Is this part of the standard? Or is it unspecified?
>> Is the rationale explained somewhere?
>>
>> PS: I tested this only with VS2005
>> regards,
>
> It is part of the Standard. It is specified. The rationale is simple: if
> it were allowed, then you would only need to derive from the class that
> was granted friendship to gain access. Friendship is explicit. If it
> were inherited, the friendship would be open-ended, and I don't know whom
> else I'm granting friendship besides 'A', if any class deriving from 'A'
> would inherit it. Makes sense?
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask