By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

access to private members from inner classes

P: n/a
Hello,

I want to write a list class with an iterator class as an inner class. The
iterator class must have access to certain private members of the list
class in order to do its job. Here is a reduced code example:
class List {
private:
void *rootNode;

class Iterator {
private:
void *currentNode;

public:
Iterator( List &listRef ) : currentNode( listRef.rootNode ) {}
};
};

I wouldn't think that this is a problem. The inner class is like a member of
the outer class. Other members of the outer class (e.g., member functions)
also have access to private members of the outer class. But surprisingly,
g++ 2.95.4 complains about the list class members being private.

The problem now is not only that I don't understand and don't want this
behaviour; the problem is also that I cannot imagine a proper workaround.

I'm happy for any comments clarifying this situation.

Wolfgang
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message
news:bh***********@ID-77306.news.uni-berlin.de...
Hello,

I want to write a list class with an iterator class as an inner class. The
iterator class must have access to certain private members of the list
class in order to do its job. Here is a reduced code example:
class List {
private:
void *rootNode;

class Iterator {
private:
void *currentNode;

public:
Iterator( List &listRef ) : currentNode( listRef.rootNode ) {}
};
};

I wouldn't think that this is a problem. The inner class is like a member of the outer class. Other members of the outer class (e.g., member functions)
also have access to private members of the outer class. But surprisingly,
g++ 2.95.4 complains about the list class members being private.
That's correct. Member class have no special access to the enclosing class,
never have.

The problem now is not only that I don't understand and don't want this
behaviour; the problem is also that I cannot imagine a proper workaround.

Use friendship.

class List {
private:

class Iterator;
friend class Iterator;
class Iterator {
};
};
I'm happy for any comments clarifying this situation.

Wolfgang


john
Jul 19 '05 #2

P: n/a
John Harrison wrote:
"Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message
news:bh***********@ID-77306.news.uni-berlin.de...
Hello,

I want to write a list class with an iterator class as an inner class.
The iterator class must have access to certain private members of the
list class in order to do its job. Here is a reduced code example:
class List {
private:
void *rootNode;

class Iterator {
private:
void *currentNode;

public:
Iterator( List &listRef ) : currentNode( listRef.rootNode )
{}
};
};

I wouldn't think that this is a problem. The inner class is like a member
of the outer class. Other members of the outer class (e.g., member
functions) also have access to private members of the outer class. But
surprisingly, g++ 2.95.4 complains about the list class members being
private.


That's correct. Member class have no special access to the enclosing
class, never have.


So the concept of member classes is only for namespace purposes?
The problem now is not only that I don't understand and don't want this
behaviour; the problem is also that I cannot imagine a proper workaround.


Use friendship.

class List {
private:

class Iterator;
friend class Iterator;
class Iterator {
};
};


Oh, I didn't know that friendship is also useable for member classes. I
always thought it was only for non-member functions.
I'm happy for any comments clarifying this situation.

Wolfgang


john


Thank you very much for your fast and helpful response.

Wolfgang
Jul 19 '05 #3

P: n/a
> >
That's correct. Member class have no special access to the enclosing
class, never have.


So the concept of member classes is only for namespace purposes?


That's about it.
The problem now is not only that I don't understand and don't want this
behaviour; the problem is also that I cannot imagine a proper workaround.


Use friendship.

class List {
private:

class Iterator;
friend class Iterator;
class Iterator {
};
};


Oh, I didn't know that friendship is also useable for member classes. I
always thought it was only for non-member functions.


Friendship is for entire classes (members or not) or for non-member
functions.

john
Jul 19 '05 #4

P: n/a
foo
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bh***********@ID-196037.news.uni-berlin.de>...
"Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message
news:bh***********@ID-77306.news.uni-berlin.de...
Hello,

I want to write a list class with an iterator class as an inner class. The
iterator class must have access to certain private members of the list
class in order to do its job. Here is a reduced code example:
class List {
private:
void *rootNode;

class Iterator {
private:
void *currentNode;

public:
Iterator( List &listRef ) : currentNode( listRef.rootNode ) {}
};
};

I wouldn't think that this is a problem. The inner class is like a member

of
the outer class. Other members of the outer class (e.g., member functions)
also have access to private members of the outer class. But surprisingly,
g++ 2.95.4 complains about the list class members being private.


That's correct. Member class have no special access to the enclosing class,
never have.

The problem now is not only that I don't understand and don't want this
behaviour; the problem is also that I cannot imagine a proper workaround.


Use friendship.

class List {
private:

class Iterator;
friend class Iterator;
class Iterator {
};
};
I'm happy for any comments clarifying this situation.

Wolfgang


john


You don't need forward class declaration in order to declare a friend class.

class List {
private:

//class Iterator; //**** This is not needed ****
friend class Iterator;
class Iterator {
};
};
Jul 19 '05 #5

P: n/a
foo wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:<bh***********@ID-196037.news.uni-berlin.de>...
[...]

Use friendship.

class List {
private:

class Iterator;
friend class Iterator;
class Iterator {
};
};

[...]


You don't need forward class declaration in order to declare a friend
class.

class List {
private:

//class Iterator; //**** This is not needed ****
friend class Iterator;
class Iterator {
};
};


Yes, I assume that
friend class Iterator;
declares Iterator like
class Iterator;
does. So you can just add the "friend" to the forward declaration. You can
even get rid of the forward declaration and just add the "friend" to the
definition(?):
class List {
private:
[private members of List]

public:
friend class Iterator {
[members of Iterator]
};
};
(I thought, this could work maybe, tried it out and yes, it did.)

Wolfgang
Jul 19 '05 #6

P: n/a

"foo" <ma*******@axter.com> wrote in message
news:c1**************************@posting.google.c om...
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bh***********@ID-196037.news.uni-berlin.de>...
"Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message
news:bh***********@ID-77306.news.uni-berlin.de...
Hello,

I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list
class in order to do its job. Here is a reduced code example:
class List {
private:
void *rootNode;

class Iterator {
private:
void *currentNode;

public:
Iterator( List &listRef ) : currentNode( listRef.rootNode ) {} };
};

I wouldn't think that this is a problem. The inner class is like a member
of
the outer class. Other members of the outer class (e.g., member
functions) also have access to private members of the outer class. But surprisingly, g++ 2.95.4 complains about the list class members being private.


That's correct. Member class have no special access to the enclosing

class, never have.

The problem now is not only that I don't understand and don't want this behaviour; the problem is also that I cannot imagine a proper workaround.


Use friendship.

class List {
private:

class Iterator;
friend class Iterator;
class Iterator {
};
};
I'm happy for any comments clarifying this situation.

Wolfgang


john


You don't need forward class declaration in order to declare a friend

class.
class List {
private:

//class Iterator; //**** This is not needed ****
friend class Iterator;
class Iterator {
};
};


That's depends on your compiler, since it was a new concept for the OP I
thought I better be on the safe side.

john
Jul 19 '05 #7

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh***********@ID-196037.news.uni-berlin.de...

That's correct. Member class have no special access to the enclosing
class, never have.


So the concept of member classes is only for namespace purposes?


That's about it.


Not exactly.
Namespaces merely group names into distinct areas.
Namespaces do not by themselves provide access control, whereas classes do.
So if you need to control access rights to your class then use nested
classes.

--
JS

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.