473,289 Members | 1,929 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,289 software developers and data experts.

Can nested class members access private members of nesting class?

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;
// other members
private:
class Node;
Node * header;
Node * tailer;
int counter;
};

Nested class Node has all of its members as public so that Container
members can access them. And then I have the nested Class
ContainerIterator, which have both public and private parts.

template <typename T>
class Container<T>::ContainerIterator {

friend class List<T>; // so that List<T> can access private members of
Iterator
public:
// public members
private:
List<T>::Node * dummyheader;
List<T>::Node * ptr;
ContainerIterator(const List<T> & l , List<T>::LNode * p);
// private members
};

I forget to declare ContainerIterator class to be a friend of
Container class, yet member functions of ContainerIterator can access
private members of Container class! For example,
ContainerIterator(const List<T> & l , List<T>::LNode * p)
{
dummyheader = l.header; // ! This works! but why?
ptr = p;
};

So am I misinterpreting the books or is my compiler not following the
standard? BTW, I am using g++ 3.2.3. I am quite confused to be honest
and would appreciate any help very much. Thanks in advance.
Jul 22 '05 #1
8 3919
CoolPint wrote:
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.
OK, we are saying about class *Container* with some private members.
template <typename T>
class Container {
// NO friendship given to any other
public:
class ContainerIterator;
// other members
private:
class Node;
Node * header;
Node * tailer;
int counter;
};

Nested class Node has all of its members as public so that Container
members can access them. And then I have the nested Class
ContainerIterator, which have both public and private parts.
OK, now we are saying about nested class ContainerIterator nested to class
*Container*.
template <typename T>
class Container<T>::ContainerIterator {

friend class List<T>; // so that List<T> can access private members of
Iterator
public:
// public members
private:
List<T>::Node * dummyheader;
List<T>::Node * ptr;
ContainerIterator(const List<T> & l , List<T>::LNode * p);
// private members
};

I forget to declare ContainerIterator class to be a friend of
Container class, yet member functions of ContainerIterator can access
private members of Container class! For example,
ContainerIterator(const List<T> & l , List<T>::LNode * p)
{
dummyheader = l.header; // ! This works! but why?
ptr = p;
};


You access members of List class, not Container class!

I'm satisfied that if you write other constructor...

ContainerIterator(const Container<T> & l , Container<T>::LNode * p)
{
//!!! dummyheader = l.header; // ! This won't work
ptr = p;
};

....the compiler will report error.
--
mP

http://pivoluska.matfyz.cz/
Jul 22 '05 #2
Is Container the same as List? You've shown the definition of one,
then accessed a member of the other.

-Jeff

co******@yahoo.co.uk (CoolPint) wrote in message news:<15**************************@posting.google. com>...
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;
// other members
private:
class Node;
Node * header;
Node * tailer;
int counter;
};

Nested class Node has all of its members as public so that Container
members can access them. And then I have the nested Class
ContainerIterator, which have both public and private parts.

template <typename T>
class Container<T>::ContainerIterator {

friend class List<T>; // so that List<T> can access private members of
Iterator
public:
// public members
private:
List<T>::Node * dummyheader;
List<T>::Node * ptr;
ContainerIterator(const List<T> & l , List<T>::LNode * p);
// private members
};

I forget to declare ContainerIterator class to be a friend of
Container class, yet member functions of ContainerIterator can access
private members of Container class! For example,
ContainerIterator(const List<T> & l , List<T>::LNode * p)
{
dummyheader = l.header; // ! This works! but why?
ptr = p;
};

So am I misinterpreting the books or is my compiler not following the
standard? BTW, I am using g++ 3.2.3. I am quite confused to be honest
and would appreciate any help very much. Thanks in advance.

Jul 22 '05 #3
je******@comcast.net (Jeff) wrote in message news:<a8*************************@posting.google.c om>...
Is Container the same as List? You've shown the definition of one,
then accessed a member of the other.


Sorry about the naming. Yes, List is supposed to be Container. I made
an typing error while trying to copy and paste the section of codes.
Since I was getting confused myself, I found a simpler sample which
demonstrate the point I am trying to ask.

According to the books, the code below should not be compiled since
nested class "inner" is accessing private member of nesting class
"enclose" without being declared a friend, but my compiler doesn't say
anything.

I want to know if this is the standard behaviour and I made a wrong
interpretation of the books or it is my compiler doing something
weird.
class enclose
{
public:
class inner
{
public:
void f(enclose *e)
{
e->x = 1;
e->y = 2;
e->z = 3;
}
};
int x;
protected:
int y;
private:
int z;
};
Jul 22 '05 #4
This one was a surprise to me, too!

http://www.gnu.org/software/gcc/bugs.html#nonbugs_cxx

-Jeff

CoolPint wrote:
je******@comcast.net (Jeff) wrote in message news:<a8*************************@posting.google.c om>...
Is Container the same as List? You've shown the definition of one,
then accessed a member of the other.

Sorry about the naming. Yes, List is supposed to be Container. I made
an typing error while trying to copy and paste the section of codes.
Since I was getting confused myself, I found a simpler sample which
demonstrate the point I am trying to ask.

According to the books, the code below should not be compiled since
nested class "inner" is accessing private member of nesting class
"enclose" without being declared a friend, but my compiler doesn't say
anything.

I want to know if this is the standard behaviour and I made a wrong
interpretation of the books or it is my compiler doing something
weird.
class enclose
{
public:
class inner
{
public:
void f(enclose *e)
{
e->x = 1;
e->y = 2;
e->z = 3;
}
};
int x;
protected:
int y;
private:
int z;
};


Jul 22 '05 #5
Jeff Schwab wrote:
This one was a surprise to me, too!


Unfortunately, it won't be the last ....

Jul 22 '05 #6
This one was a surprise to me, too!


Unfortunately, it won't be the last ....


By definition, since I'd be surprised if it wasn't. :)

Jul 22 '05 #7
> Jeff Schwab wrote:
This one was a surprise to me, too!


Unfortunately, it won't be the last ....


The page you pointed to me says "Non-bugs"...
Does this mean it's the standard to grant nested classes access to
private members? (Which means the books I am reading have wrong
information)

Or is g++ specific behaviour (which means g++ doesn't follow the
standard)?
Jul 22 '05 #8
CoolPint wrote:
Jeff Schwab wrote:
This one was a surprise to me, too!


Unfortunately, it won't be the last ....

The page you pointed to me says "Non-bugs"...
Does this mean it's the standard to grant nested classes access to
private members? (Which means the books I am reading have wrong
information)

Or is g++ specific behaviour (which means g++ doesn't follow the
standard)?


I believe this means the standard *did not* allow this behavior, but
someone felt it should. So, they submitted a defect report, and
proposed the following resolution:

In 11.8 class.access.nest paragraph 1, change

The members of a nested class have no special access to members of
an enclosing class, nor to classes or functions that have granted
friendship to an enclosing class; the usual access rules (clause 11
class.access) shall be obeyed.

to

A nested class is a member and as such has the same access rights
as any other member.

However, I found this among the C++ Standard Core Language Active
Issues, Revision 28 (Nov. 15, 2003):

Drafting notes:

The resolution of core issue 45 (DR) deletes 11.8 class.access.nest

So, it looks like the standard might just leave the whole thing
unspecified... Can that be right? Anyway, I think the books you got
were right, but that the standard is being changed to allow nested
classes access to all members of the enclosing class.

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
6
by: A | last post by:
Hi, How do you make use of nested functions in C++? I realize in C++ that everything must be declared first in a header file before implementation in a .cpp file. I tried to nest a method...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
9
by: Lonewolf | last post by:
Hi everyone.. I was trying to implement callback fucntions in native codes in my VC8 class, and referred to some online codes which use native class nested within managed class. basically of the...
5
by: request | last post by:
I have a little piece of code that compiles fine but I think it shouldn't compile fine. Here it is: class outer_class { public: outer_class () {} int operator () (int i1, int i2) { return...
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.