Is it standard that inner class inherits the outer class' friendship?

Discussion in 'C++' started by Qi, Apr 30, 2011.

  1. Qi

    Qi Guest

    Below code, indeed I found here,
    http://bytes.com/topic/c/answers/523273-inner-classes-friend
    I changed it a bit to make it compilable and simpler.

    The code can be compiled on both VC 2008 Express and GCC 4.5.2

    That implies that
    "An inner class can inherit its outer class' friendship".
    My question is if that implication correct?

    I doubt that because,
    1, The link I showed above seems approved that's wrong.
    Though that was posted in 2006.
    2, As far as I understand, a derived class doesn't inherit
    its base class' friendship. B is F's friend, D is derived from
    B, D is not F's friend by default. (am I correct?)

    Can any one give some summary on the friendship inheritance
    of inner and outer classes, base and derived classes that defined
    by the standard?

    Thanks


    ===CODE===

    class Node {
    private:
    int value;

    friend class Graph;
    };

    class Graph {
    private:
    class cmp {
    public:
    int operator()(const Node &l, const Node &r) {
    return (l.value - r.value); // without that friend, this
    line can't compile
    }
    };
    };


    --
    WQ
    Qi, Apr 30, 2011
    #1
    1. Advertising

  2. On Apr 30, 1:06 pm, Qi <> wrote:
    > Below code, indeed I found here,http://bytes.com/topic/c/answers/523273-inner-classes-friend
    > I changed it a bit to make it compilable and simpler.
    >
    > The code can be compiled on both VC 2008 Express and GCC 4.5.2
    >
    > That implies that
    > "An inner class can inherit its outer class' friendship".
    > My question is if that implication correct?
    >
    > I doubt that because,
    > 1, The link I showed above seems approved that's wrong.
    > Though that was posted in 2006.
    > 2, As far as I understand, a derived class doesn't inherit
    > its base class' friendship. B is F's friend, D is derived from
    > B, D is not F's friend by default. (am I correct?)
    >
    > Can any one give some summary on the friendship inheritance
    > of inner and outer classes, base and derived classes that defined
    > by the standard?
    >
    > Thanks
    >
    > ===CODE===
    >
    > class Node {
    > private:
    >      int value;
    >
    >      friend class Graph;
    >
    > };
    >
    > class Graph {
    > private:
    >      class cmp {
    >      public:
    >          int operator()(const Node &l, const Node &r) {
    >              return (l.value - r.value); // without that friend, this
    > line can't compile
    >          }
    >      };
    >
    > };
    >
    > --
    > WQ


    Hello
    1. At first, I have to say the terms "inner class" and "outer class"
    aren't standard. It's somehow Java terms. C++ uses "nested class".
    2. About your code, By the term "inner class", if you mean "nested
    class",
    you didn't use it. You have two classes: Node and Graph and you
    declared
    Graph as a friend of Node. May be, by inner you mean the logical
    inner,
    because each node of a graph is somehow the inner part of whole graph.
    3. Yes. You are right. friendship isn't inherited, transitive, or
    reciprocal.
    Please see the following link:
    http://www.parashift.com/c -faq-lite/friends.html#faq-14.4
    Also for standard wording, see section 11.3 of
    N3290- Final Draft of International Standard.

    HTH,
    -- Saeed Amrollahi
    Saeed Amrollahi, Apr 30, 2011
    #2
    1. Advertising

  3. Qi

    Qi Guest

    On 2011-4-30 21:58, Saeed Amrollahi wrote:
    >> class Graph {
    >> private:
    >> class cmp {
    >> public:
    >> int operator()(const Node&l, const Node&r) {
    >> return (l.value - r.value); // without that friend, this
    >> line can't compile
    >> }
    >> };
    >>
    >> };
    >>

    > 1. At first, I have to say the terms "inner class" and "outer class"
    > aren't standard. It's somehow Java terms. C++ uses "nested class".
    > 2. About your code, By the term "inner class", if you mean "nested
    > class",
    > you didn't use it. You have two classes: Node and Graph and you
    > declared
    > Graph as a friend of Node. May be, by inner you mean the logical
    > inner,
    > because each node of a graph is somehow the inner part of whole graph.



    Thanks for the reply and sorry for the misunderstand.
    For nested class (OK, now I will use this term, but then what's the
    equivalence of "outer class"?), I mean Graph::cmp.

    Sorry for any misunderstand.


    --
    WQ
    Qi, Apr 30, 2011
    #3
  4. On Apr 30, 6:08 pm, Qi <> wrote:
    > On 2011-4-30 21:58, Saeed Amrollahi wrote:
    >
    >
    >
    > >> class Graph {
    > >> private:
    > >>       class cmp {
    > >>       public:
    > >>           int operator()(const Node&l, const Node&r) {
    > >>               return (l.value - r.value); // without that friend, this
    > >> line can't compile
    > >>           }
    > >>       };

    >
    > >> };

    >
    > > 1. At first, I have to say the terms "inner class" and "outer class"
    > > aren't standard. It's somehow Java terms. C++ uses "nested class".
    > > 2. About your code, By the term "inner class", if you mean "nested
    > > class",
    > > you didn't use it. You have two classes: Node and Graph and you
    > > declared
    > > Graph as a friend of Node. May be, by inner you mean the logical
    > > inner,
    > > because each node of a graph is somehow the inner part of whole graph.

    >
    > Thanks for the reply and sorry for the misunderstand.
    > For nested class (OK, now I will use this term, but then what's the
    > equivalence of "outer class"?), I mean Graph::cmp.
    >
    > Sorry for any misunderstand.
    >
    > --
    > WQ


    Hi
    I'm sorry too, because, I didn't pay attention to
    nested class Graph::cmp.
    As I mentioned in section 11.3 at paragraph 2, there is wording:
    "Declaring a class to be a friend implies that the names of
    private and protected members from the class granting friendship
    can be accessed in the base-specifiers and member declarations of
    the befriended class."
    In your case:
    If you declare class Graph a friend of class Node, the private
    names of class Node (the class granting friendship) can be accessed
    in nested class cmp of the befriending class (Graph).

    BTW, I can't find a standard terminology for "Outer class",
    but in standard document, there is an example using "enclose" as
    class name.

    HTH,
    -- Saeed Amrollahi
    Saeed Amrollahi, Apr 30, 2011
    #4
  5. Qi

    Qi Guest

    On 2011-5-1 3:37, Saeed Amrollahi wrote:
    > As I mentioned in section 11.3 at paragraph 2, there is wording:
    > "Declaring a class to be a friend implies that the names of
    > private and protected members from the class granting friendship
    > can be accessed in the base-specifiers and member declarations of
    > the befriended class."
    > In your case:
    > If you declare class Graph a friend of class Node, the private
    > names of class Node (the class granting friendship) can be accessed
    > in nested class cmp of the befriending class (Graph).


    Thanks, that explained my problem.


    --
    WQ
    Qi, May 1, 2011
    #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. Yamin
    Replies:
    4
    Views:
    16,672
    Yamin
    Oct 24, 2004
  2. Ahan Hsieh
    Replies:
    2
    Views:
    565
    Ahan Hsieh
    Oct 5, 2007
  3. Juha Nieminen
    Replies:
    9
    Views:
    912
    Kai-Uwe Bux
    Jun 13, 2011
  4. John Gordon
    Replies:
    5
    Views:
    437
    Adam Skutt
    May 9, 2012
  5. Replies:
    2
    Views:
    226
Loading...

Share This Page