Can nested class members access private members of nesting class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CoolPint

    Can nested class members access private members of nesting class?

    I read in books that nested class cannot access private members of
    nesting class and vice versa unless they are made friends. Somehow, my
    compiler is letting my nested class member functions access private
    members of nesting class.

    template <typename T>
    class Container {
    // NO friendship given to any other
    public:
    class ContainerIterat or;
    // other members
    private:
    class Node;
    Node * header;
    Node * tailer;
    int counter;
    };

    Nested class Node has all of its members as public so that Container
    members can access them. And then I have the nested Class
    ContainerIterat or, which have both public and private parts.

    template <typename T>
    class Container<T>::C ontainerIterato r {

    friend class List<T>; // so that List<T> can access private members of
    Iterator
    public:
    // public members
    private:
    List<T>::Node * dummyheader;
    List<T>::Node * ptr;
    ContainerIterat or(const List<T> & l , List<T>::LNode * p);
    // private members
    };

    I forget to declare ContainerIterat or class to be a friend of
    Container class, yet member functions of ContainerIterat or can access
    private members of Container class! For example,
    ContainerIterat or(const List<T> & l , List<T>::LNode * p)
    {
    dummyheader = l.header; // ! This works! but why?
    ptr = p;
    };

    So am I misinterpreting the books or is my compiler not following the
    standard? BTW, I am using g++ 3.2.3. I am quite confused to be honest
    and would appreciate any help very much. Thanks in advance.
  • Matej Pivoluska

    #2
    Re: Can nested class members access private members of nesting class?

    CoolPint wrote:
    [color=blue]
    > I read in books that nested class cannot access private members of
    > nesting class and vice versa unless they are made friends. Somehow, my
    > compiler is letting my nested class member functions access private
    > members of nesting class.[/color]

    OK, we are saying about class *Container* with some private members.
    [color=blue]
    > template <typename T>
    > class Container {
    > // NO friendship given to any other
    > public:
    > class ContainerIterat or;
    > // other members
    > private:
    > class Node;
    > Node * header;
    > Node * tailer;
    > int counter;
    > };
    >
    > Nested class Node has all of its members as public so that Container
    > members can access them. And then I have the nested Class
    > ContainerIterat or, which have both public and private parts.[/color]

    OK, now we are saying about nested class ContainerIterat or nested to class
    *Container*.
    [color=blue]
    > template <typename T>
    > class Container<T>::C ontainerIterato r {
    >
    > friend class List<T>; // so that List<T> can access private members of
    > Iterator
    > public:
    > // public members
    > private:
    > List<T>::Node * dummyheader;
    > List<T>::Node * ptr;
    > ContainerIterat or(const List<T> & l , List<T>::LNode * p);
    > // private members
    > };
    >
    > I forget to declare ContainerIterat or class to be a friend of
    > Container class, yet member functions of ContainerIterat or can access
    > private members of Container class! For example,
    > ContainerIterat or(const List<T> & l , List<T>::LNode * p)
    > {
    > dummyheader = l.header; // ! This works! but why?
    > ptr = p;
    > };[/color]

    You access members of List class, not Container class!

    I'm satisfied that if you write other constructor...

    ContainerIterat or(const Container<T> & l , Container<T>::L Node * p)
    {
    //!!! dummyheader = l.header; // ! This won't work
    ptr = p;
    };

    ....the compiler will report error.


    --
    mP


    Comment

    • Jeff

      #3
      Re: Can nested class members access private members of nesting class?

      Is Container the same as List? You've shown the definition of one,
      then accessed a member of the other.

      -Jeff

      coolpint@yahoo. co.uk (CoolPint) wrote in message news:<159a5c32. 0312122340.2074 968c@posting.go ogle.com>...[color=blue]
      > I read in books that nested class cannot access private members of
      > nesting class and vice versa unless they are made friends. Somehow, my
      > compiler is letting my nested class member functions access private
      > members of nesting class.
      >
      > template <typename T>
      > class Container {
      > // NO friendship given to any other
      > public:
      > class ContainerIterat or;
      > // other members
      > private:
      > class Node;
      > Node * header;
      > Node * tailer;
      > int counter;
      > };
      >
      > Nested class Node has all of its members as public so that Container
      > members can access them. And then I have the nested Class
      > ContainerIterat or, which have both public and private parts.
      >
      > template <typename T>
      > class Container<T>::C ontainerIterato r {
      >
      > friend class List<T>; // so that List<T> can access private members of
      > Iterator
      > public:
      > // public members
      > private:
      > List<T>::Node * dummyheader;
      > List<T>::Node * ptr;
      > ContainerIterat or(const List<T> & l , List<T>::LNode * p);
      > // private members
      > };
      >
      > I forget to declare ContainerIterat or class to be a friend of
      > Container class, yet member functions of ContainerIterat or can access
      > private members of Container class! For example,
      > ContainerIterat or(const List<T> & l , List<T>::LNode * p)
      > {
      > dummyheader = l.header; // ! This works! but why?
      > ptr = p;
      > };
      >
      > So am I misinterpreting the books or is my compiler not following the
      > standard? BTW, I am using g++ 3.2.3. I am quite confused to be honest
      > and would appreciate any help very much. Thanks in advance.[/color]

      Comment

      • CoolPint

        #4
        Re: Can nested class members access private members of nesting class?

        jeffplus@comcas t.net (Jeff) wrote in message news:<a81f684.0 312131021.1cf68 90c@posting.goo gle.com>...[color=blue]
        > Is Container the same as List? You've shown the definition of one,
        > then accessed a member of the other.[/color]

        Sorry about the naming. Yes, List is supposed to be Container. I made
        an typing error while trying to copy and paste the section of codes.
        Since I was getting confused myself, I found a simpler sample which
        demonstrate the point I am trying to ask.

        According to the books, the code below should not be compiled since
        nested class "inner" is accessing private member of nesting class
        "enclose" without being declared a friend, but my compiler doesn't say
        anything.

        I want to know if this is the standard behaviour and I made a wrong
        interpretation of the books or it is my compiler doing something
        weird.


        class enclose
        {
        public:
        class inner
        {
        public:
        void f(enclose *e)
        {
        e->x = 1;
        e->y = 2;
        e->z = 3;
        }
        };
        int x;
        protected:
        int y;
        private:
        int z;
        };

        Comment

        • Jeff Schwab

          #5
          Re: Can nested class members access private members of nesting class?

          This one was a surprise to me, too!



          -Jeff

          CoolPint wrote:[color=blue]
          > jeffplus@comcas t.net (Jeff) wrote in message news:<a81f684.0 312131021.1cf68 90c@posting.goo gle.com>...
          >[color=green]
          >>Is Container the same as List? You've shown the definition of one,
          >>then accessed a member of the other.[/color]
          >
          >
          > Sorry about the naming. Yes, List is supposed to be Container. I made
          > an typing error while trying to copy and paste the section of codes.
          > Since I was getting confused myself, I found a simpler sample which
          > demonstrate the point I am trying to ask.
          >
          > According to the books, the code below should not be compiled since
          > nested class "inner" is accessing private member of nesting class
          > "enclose" without being declared a friend, but my compiler doesn't say
          > anything.
          >
          > I want to know if this is the standard behaviour and I made a wrong
          > interpretation of the books or it is my compiler doing something
          > weird.
          >
          >
          > class enclose
          > {
          > public:
          > class inner
          > {
          > public:
          > void f(enclose *e)
          > {
          > e->x = 1;
          > e->y = 2;
          > e->z = 3;
          > }
          > };
          > int x;
          > protected:
          > int y;
          > private:
          > int z;
          > };[/color]

          Comment

          • Gianni Mariani

            #6
            Re: Can nested class members access private members of nesting class?

            Jeff Schwab wrote:[color=blue]
            > This one was a surprise to me, too!
            >[/color]

            Unfortunately, it won't be the last ....

            Comment

            • Jeff Schwab

              #7
              Re: Can nested class members access private members of nesting class?

              [color=blue][color=green]
              >> This one was a surprise to me, too![/color]
              >
              > Unfortunately, it won't be the last ....[/color]

              By definition, since I'd be surprised if it wasn't. :)

              Comment

              • CoolPint

                #8
                Re: Can nested class members access private members of nesting class?

                > Jeff Schwab wrote:[color=blue][color=green]
                > > This one was a surprise to me, too!
                > >[/color]
                >
                > Unfortunately, it won't be the last ....[/color]

                The page you pointed to me says "Non-bugs"...
                Does this mean it's the standard to grant nested classes access to
                private members? (Which means the books I am reading have wrong
                information)

                Or is g++ specific behaviour (which means g++ doesn't follow the
                standard)?

                Comment

                • Jeff Schwab

                  #9
                  Re: Can nested class members access private members of nesting class?

                  CoolPint wrote:[color=blue][color=green]
                  >>Jeff Schwab wrote:
                  >>[color=darkred]
                  >>>This one was a surprise to me, too!
                  >>>[/color]
                  >>
                  >>Unfortunately , it won't be the last ....[/color]
                  >
                  >
                  > The page you pointed to me says "Non-bugs"...
                  > Does this mean it's the standard to grant nested classes access to
                  > private members? (Which means the books I am reading have wrong
                  > information)
                  >
                  > Or is g++ specific behaviour (which means g++ doesn't follow the
                  > standard)?[/color]

                  I believe this means the standard *did not* allow this behavior, but
                  someone felt it should. So, they submitted a defect report, and
                  proposed the following resolution:

                  In 11.8 class.access.ne st paragraph 1, change

                  The members of a nested class have no special access to members of
                  an enclosing class, nor to classes or functions that have granted
                  friendship to an enclosing class; the usual access rules (clause 11
                  class.access) shall be obeyed.

                  to

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

                  However, I found this among the C++ Standard Core Language Active
                  Issues, Revision 28 (Nov. 15, 2003):

                  Drafting notes:

                  The resolution of core issue 45 (DR) deletes 11.8 class.access.ne st

                  So, it looks like the standard might just leave the whole thing
                  unspecified... Can that be right? Anyway, I think the books you got
                  were right, but that the standard is being changed to allow nested
                  classes access to all members of the enclosing class.

                  Comment

                  Working...