Re: friendship not inheritable
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