friendship not inheritable

Discussion in 'C++' started by Hicham Mouline, Nov 14, 2008.

  1. 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,
    Hicham Mouline, Nov 14, 2008
    #1
    1. Advertising

  2. On Nov 14, 3:07 pm, "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;
    >
    > };


    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
    Maxim Yegorushkin, Nov 14, 2008
    #2
    1. Advertising

  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" <> wrote in message
    news:gfk54k$rce$...
    > 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
    Hicham Mouline, Nov 14, 2008
    #3
  4. Hicham Mouline

    red floyd Guest

    On Nov 14, 7:07 am, "Hicham Mouline" <> 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?
    red floyd, Nov 14, 2008
    #4
  5. :)
    What about the posted suggestion though?

    "red floyd" <> wrote in message
    news:...
    On Nov 14, 7:07 am, "Hicham Mouline" <> 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, Nov 17, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saumya
    Replies:
    11
    Views:
    4,243
    Datta Patil
    Mar 2, 2004
  2. Fabiano Sidler

    Why are so many built-in types inheritable?

    Fabiano Sidler, Mar 18, 2006, in forum: Python
    Replies:
    17
    Views:
    478
    Antoon Pardon
    Mar 31, 2006
  3. Indraseena

    Making base class non-inheritable

    Indraseena, Jun 22, 2006, in forum: C++
    Replies:
    6
    Views:
    469
    Indraseena
    Jun 26, 2006
  4. kj
    Replies:
    2
    Views:
    238
  5. Alexander Walker
    Replies:
    4
    Views:
    122
    Steven Cheng[MSFT]
    Feb 27, 2006
Loading...

Share This Page