473,804 Members | 3,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.rootNod e ) {}
};
};

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 9764

"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.rootNod e ) {}
};
};

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.rootNod e )
{}
};
};

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.rootNod e ) {}
};
};

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*******@axte r.com> wrote in message
news:c1******** *************** ***@posting.goo gle.com...
"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.rootNod e ) {} };
};

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
3947
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 private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
9
1543
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
2431
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 outer_data;
4
4646
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 or field public? Thanks, Dennis
10
25744
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 ) Is there a way to get this information ? Thnaks in advance
5
2693
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 that's come before. To take an extreme example, suppose you have been writing Visual Basic applications for years now. If you're like many developers, you will have built up a substantial inventory of code in that time. And if you've been following...
9
1861
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 to the order */ }
2
1870
by: Carlos Martinez | last post by:
Hi all: Supose the following classes: class A { class InnerA1 { };
6
5035
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.