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

From: Matej Pivoluska (spam_at_spam.com)
Date: 12/13/03


Date: Sat, 13 Dec 2003 13:32:20 +0100

CoolPint wrote:

> 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.

OK, we are saying about class *Container* with some private members.

> template <typename T>
> class Container {
> // NO friendship given to any other
> public:
> class ContainerIterator;
> // 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
> ContainerIterator, which have both public and private parts.

OK, now we are saying about nested class ContainerIterator nested to class
*Container*.

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

You access members of List class, not Container class!

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

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

...the compiler will report error.

-- 
mP
http://pivoluska.matfyz.cz/


Relevant Pages

  • Re: Confirm bad news
    ... nested class within the same enclosing class. ... class B {private int secret;} ... members have scope throughout the compilation ...
    (comp.lang.java.programmer)
  • Re: Need help with calling one class from another
    ... is an object within the other class (InventoryItem). ... The line you say doesn't work isn't accessing the private field, ... I admit, from the code you posted, it's not obvious to me why you have the nested class anyway. ... That said, the issue you're seeing is a basic rule about nested classes: the nested class can see "private" members of the containing class, but not the other way around. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need help with calling one class from another
    ... isn't accessing the private field, ... value to the nested class anyway. ... the nested class can see "private" members of the containing class, ... private variables. ...
    (microsoft.public.dotnet.languages.csharp)
  • Can nested class members access private members of nesting class?
    ... compiler is letting my nested class member functions access private ... members of nesting class. ... Nested class Node has all of its members as public so that Container ...
    (comp.lang.cpp)
  • Re: Can nested class members access private members of nesting class?
    ... Is Container the same as List? ... > compiler is letting my nested class member functions access private ... > members of nesting class. ...
    (comp.lang.cpp)