473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

friendship not inheritable

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,
Nov 14 '08 #1
6 1751
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
Nov 14 '08 #2
On Nov 14, 3:07*pm, "Hicham Mouline" <hic...@mouline.orgwrote:
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
Nov 14 '08 #3
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.********@comAcast.netwrote in message
news:gf**********@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

Nov 14 '08 #4
On Nov 14, 7:07*am, "Hicham Mouline" <hic...@mouline.orgwrote:
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?

Nov 14 '08 #5
:-)
What about the posted suggestion though?

"red floyd" <re******@gmail.comwrote in message
news:5b**********************************@e1g2000p ra.googlegroups.com...
On Nov 14, 7:07 am, "Hicham Mouline" <hic...@mouline.orgwrote:
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?
Nov 17 '08 #6
Hicham Mouline wrote:
:-)
What about the posted suggestion though?

"red floyd" <re******@gmail.comwrote in message
news:5b**********************************@e1g2000p ra.googlegroups.com...
On Nov 14, 7:07 am, "Hicham Mouline" <hic...@mouline.orgwrote:
>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
Nov 18 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
Hi all, quick question, how do I make a define a class as being inheritable only?? Thanks Mark
4
by: Marcin Vorbrodt | last post by:
Is friendship inherited? Meaning if: class Base { public: friend FriendClass; }; class Derived : public Base { }
7
by: Erik | last post by:
private and protected members can only be accessed by deriving a class or by giving friendship. The problem with friendship is that friend statements are written inside the class. A FriendShip...
4
by: JH Trauntvein | last post by:
Consider the following example: namespace n1 { class cn1_base; namespace n1_helpers { class helper1 {
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
3
by: tom.s.smith | last post by:
I know that friendship isn't inherited in C++, and have two questions. (1) Why? Is it a technical problem, or just by design? If the latter, isn't this a little prescriptive, and shouldn't there...
2
by: Mark P | last post by:
Is there a way to do anything like the following: class A { friend void B::foo(); ... }; class B
4
by: werasm | last post by:
I've read the following somewhere: "Friendship is the strongest form of coupling there is. That is, you introduce a high degree of dependency when you declare a friend, since the friend becomes...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.