[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

friendship not inheritable

Hicham Mouline

11/14/2008 3:08:00 PM

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,


6 Answers

Victor Bazarov

11/14/2008 3:23:00 PM

0

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

Maxim Yegorushkin

11/14/2008 3:35:00 PM

0

On Nov 14, 3:07 pm, "Hicham Mouline" <hic...@mouline.org> 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;
>
> };

Friendship derivation can be emulated:

class Base {
protected:
// Key is accessible by A and derived classes
enum Key { key };
friend class A;

public:
virtual void f(A*) const =0;

};

class A {
public:
// this function can only be invoked by users
// who have access to Base::Key
void foo(Base::Key);
};

class Derived : public Base {
virtual void f(A* a) const
{
a->foo(key);
}
};

--
Max

Hicham Mouline

11/14/2008 3:38:00 PM

0

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


red floyd

11/14/2008 4:46:00 PM

0

On Nov 14, 7:07 am, "Hicham Mouline" <hic...@mouline.org> wrote:
> Hello,
> A semi-skeptical colleague is asking me why friendship is not inheritable,
> specifically:
>

Think of it in terms of "real life", which in this case, C++ actually
mirrors.

Your friends' children are not necessarily your friends. And would
you want your friends' children playing with your private parts?

Hicham Mouline

11/17/2008 10:23:00 AM

0

:-)
What about the posted suggestion though?

"red floyd" <redfloyd@gmail.com> wrote in message
news:5bbea8dc-6650-4d8c-8a96-5e1472d08e32@e1g2000pra.googlegroups.com...
On Nov 14, 7:07 am, "Hicham Mouline" <hic...@mouline.org> wrote:
> Hello,
> A semi-skeptical colleague is asking me why friendship is not inheritable,
> specifically:
>
Think of it in terms of "real life", which in this case, C++ actually
mirrors.
Your friends' children are not necessarily your friends. And would
you want your friends' children playing with your private parts?


Hendrik Schober

11/18/2008 8:28:00 AM

0

Hicham Mouline wrote:
> :-)
> What about the posted suggestion though?
>
> "red floyd" <redfloyd@gmail.com> wrote in message
> news:5bbea8dc-6650-4d8c-8a96-5e1472d08e32@e1g2000pra.googlegroups.com...
> On Nov 14, 7:07 am, "Hicham Mouline" <hic...@mouline.org> wrote:
>> Hello,
>> A semi-skeptical colleague is asking me why friendship is not inheritable,
>> specifically:
>>
> Think of it in terms of "real life", which in this case, C++ actually
> mirrors.
> Your friends' children are not necessarily your friends. And would
> you want your friends' children playing with your private parts?

And from the other POV: Would you want to inherit your parent's
friends? :)

Schobi