C
CoolPint
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 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.
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;
};
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 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.
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;
};
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.