472,353 Members | 972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

access to private members from inner classes

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

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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
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...
9
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
5
by: Fabio Rossi | last post by:
Hi! I'm learning C++ from the book "Thinking in C++". Now I'm reading about nested classes and access control. I have written this code #include...
4
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order...
2
by: Carlos Martinez | last post by:
Hi all: Supose the following classes: class A { class InnerA1 { };
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.