Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > friendship not inheritable

Reply
Thread Tools

friendship not inheritable

 
 
Hicham Mouline
Guest
Posts: n/a
 
      11-14-2008
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,


 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      11-14-2008
On Nov 14, 3:07*pm, "Hicham Mouline" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Hicham Mouline
Guest
Posts: n/a
 
      11-14-2008
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" <(E-Mail Removed)> wrote in message
news:gfk54k$rce$(E-Mail Removed)...
> 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



 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-14-2008
On Nov 14, 7:07*am, "Hicham Mouline" <(E-Mail Removed)> 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?

 
Reply With Quote
 
Hicham Mouline
Guest
Posts: n/a
 
      11-17-2008

What about the posted suggestion though?

"red floyd" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Nov 14, 7:07 am, "Hicham Mouline" <(E-Mail Removed)> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheritable computed class attributes? kj Python 2 04-30-2010 10:34 PM
Making base class non-inheritable Indraseena C++ 6 06-26-2006 10:23 AM
Why are so many built-in types inheritable? Fabiano Sidler Python 17 03-31-2006 11:09 AM
HtmlHead is not inheritable how can I prevent the <title> tag from rendering? Alexander Walker ASP .Net Building Controls 4 02-27-2006 01:26 AM
How to prevent a class from being inheritable, in C++ Saumya C++ 11 03-02-2004 01:10 PM



Advertisments