access to private members from inner classes

W

Wolfgang Jeltsch

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
 
J

John Harrison

Wolfgang Jeltsch said:
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
 
W

Wolfgang Jeltsch

John said:
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?
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.

Thank you very much for your fast and helpful response.

Wolfgang
 
J

John Harrison

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

That's about it.
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
 
F

foo

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


Use friendship.

class List {
private:

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


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 {
};
};
 
W

Wolfgang Jeltsch

foo said:
John Harrison said:
[...]

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
 
J

John Harrison

foo said:
"John Harrison" <[email protected]> wrote in message

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
 
J

Josephine Schafer

John Harrison said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top