Is nested class automatically friend of class that it is nested in?

Discussion in 'C++' started by request@no_spam.com, Sep 22, 2006.

  1. I have a little piece of code that compiles fine but I think it shouldn't
    compile fine. Here it is:

    class outer_class {
    public:
    outer_class () {}

    int operator () (int i1, int i2) {
    return i1+i2;
    }

    class inner_class {
    public:
    inner_class () {}

    void inner_method (bool b) {
    int retv;
    outer_class outer_obj;

    if (b) {
    retv = outer_obj(3,5);
    } else {
    /* This here shouldn't be allowed, inner class
    is not made friend of outer class anywhere! */
    retv = outer_obj(3,5,true);
    }
    }
    };

    private:
    int operator () (int i1, int i2, bool ignored) {
    return i1 * i2;
    }
    };
     
    request@no_spam.com, Sep 22, 2006
    #1
    1. Advertising

  2. request@no_spam.com

    Bo Persson Guest

    request@no_spam.com wrote:
    > I have a little piece of code that compiles fine but I think it
    > shouldn't compile fine. Here it is:
    >
    > class outer_class {
    > public:
    > outer_class () {}
    >
    > int operator () (int i1, int i2) {
    > return i1+i2;
    > }
    >
    > class inner_class {
    > public:
    > inner_class () {}
    >
    > void inner_method (bool b) {
    > int retv;
    > outer_class outer_obj;
    >
    > if (b) {
    > retv = outer_obj(3,5);
    > } else {
    > /* This here shouldn't be allowed, inner class
    > is not made friend of outer class anywhere! */
    > retv = outer_obj(3,5,true);
    > }
    > }
    > };
    >
    > private:
    > int operator () (int i1, int i2, bool ignored) {
    > return i1 * i2;
    > }
    > };


    It is a member of outer_class, so it should have the same access
    rights as all the other members, like the member functions.


    Bo Persson
     
    Bo Persson, Sep 22, 2006
    #2
    1. Advertising

  3. On Fri, 22 Sep 2006 18:03:23 +0200, Bo Persson wrote:

    > request@no_spam.com wrote:
    >> I have a little piece of code that compiles fine but I think it
    >> shouldn't compile fine. Here it is:
    >>
    >> class outer_class {
    >> public:
    >> outer_class () {}
    >>
    >> int operator () (int i1, int i2) {
    >> return i1+i2;
    >> }
    >>
    >> class inner_class {
    >> public:
    >> inner_class () {}
    >>
    >> void inner_method (bool b) {
    >> int retv;
    >> outer_class outer_obj;
    >>
    >> if (b) {
    >> retv = outer_obj(3,5);
    >> } else {
    >> /* This here shouldn't be allowed, inner class
    >> is not made friend of outer class anywhere! */
    >> retv = outer_obj(3,5,true);
    >> }
    >> }
    >> };
    >>
    >> private:
    >> int operator () (int i1, int i2, bool ignored) {
    >> return i1 * i2;
    >> }
    >> };

    >
    > It is a member of outer_class, so it should have the same access
    > rights as all the other members, like the member functions.
    >
    > Bo Persson


    That makes sense, thank you...
     
    request@no_spam.com, Sep 22, 2006
    #3
  4. request@no_spam.com

    Fraser Ross Guest

    "Bo Persson"
    > It is a member of outer_class, so it should have the same access
    > rights as all the other members, like the member functions.



    Can someone confirm these 2 points? Can a class, which is a type, be
    considered a member? I think that it is only proposed that a nested
    class has access to the nesting classes privates. It doesn't work with
    my compiler.

    Fraser.



    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    Fraser Ross, Sep 24, 2006
    #4
  5. request@no_spam.com

    Bo Persson Guest

    Fraser Ross wrote:
    > "Bo Persson"
    >> It is a member of outer_class, so it should have the same access
    >> rights as all the other members, like the member functions.

    >
    >
    > Can someone confirm these 2 points? Can a class, which is a type,
    > be
    > considered a member? I think that it is only proposed that a nested
    > class has access to the nesting classes privates.


    You are correct, it is a proposal for the next standard. I thought it
    had passed already.

    The current standard says (11.8):

    "The members of a nested class have no special access to members of an
    enclosing class".


    The draft for the next standard says the opposite:

    "A nested class is a member and as such has the same access rights as
    any other member."


    > It doesn't work with my compiler.


    It does work with my compiler!


    Bo Persson
     
    Bo Persson, Sep 24, 2006
    #5
  6. request@no_spam.com

    T.A. Guest

    On Mon, 25 Sep 2006 00:03:07 +0200, Bo Persson wrote:

    > Fraser Ross wrote:
    >> "Bo Persson"
    >>> It is a member of outer_class, so it should have the same access
    >>> rights as all the other members, like the member functions.

    >>
    >>
    >> Can someone confirm these 2 points? Can a class, which is a type,
    >> be
    >> considered a member? I think that it is only proposed that a nested
    >> class has access to the nesting classes privates.

    >
    > You are correct, it is a proposal for the next standard. I thought it
    > had passed already.
    >
    > The current standard says (11.8):
    >
    > "The members of a nested class have no special access to members of an
    > enclosing class".
    >
    > The draft for the next standard says the opposite:
    >
    > "A nested class is a member and as such has the same access rights as
    > any other member."
    >
    >> It doesn't work with my compiler.

    >
    > It does work with my compiler!
    >
    > Bo Persson


    Yeah, it also works with mine, but I'm suspicious on many things that work
    with that particulyr compiler... For example if aou leave some setting
    default following code comiples fine:

    for (int i = 0; i < 10; ++i) {
    ...
    }
    ....
    i = 12;
    ....

    (The scope of i is not forced to for loop by default)

    Now, this is just an example...Compilers should not be trusted when
    checking is some bahvior standard or not.
     
    T.A., Sep 25, 2006
    #6
    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. Ike Naar
    Replies:
    3
    Views:
    1,232
    Jonathan Turkanis
    Feb 26, 2004
  2. Chris Schadl

    friend access from nested class

    Chris Schadl, May 5, 2004, in forum: C++
    Replies:
    1
    Views:
    375
    Siemel Naran
    May 5, 2004
  3. Replies:
    4
    Views:
    445
    Victor Bazarov
    Sep 30, 2007
  4. Replies:
    3
    Views:
    480
    Victor Bazarov
    Dec 7, 2007
  5. Peter
    Replies:
    2
    Views:
    286
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page