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