473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 9732

"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 is letting my nested class member functions access...
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 <iostream> class Outer { private: int...
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 classes, as would be the case when making the method...
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 classes.( I already have the ReflectionPermission )...
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 revolution in programming forces you to abandon all...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.