scoping of nested classes

R

Robert M. Gary

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. Example...

class A{
private:
class B{
};

class C{
MyTemplate<B> foo;
};
};

The Solaris 4.2 compiler says its a warning that C cannot access private
class B (even though its a sibling). Both the aCC (HP-UX) and the IBM (AIX)
compiler fail on this. I'm looking at page 160 of the ANSC++ spec (ISO/IEC
14882:1998(E) ) about nested classes but don't see any reference to scoping
of them. I've tried variations on friend but to no success.

-Robert
 
M

Mike Wahler

Robert M. Gary said:
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.

The private members of a class are only allowed
access by member functions of the same class,
or functions declared by that class to be friends.
A 'sibling' relationship has no special meaning.
Example...

class A{
private:
class B{
};

class C{
MyTemplate<B> foo;
};
};

The relationship between class 'B' and class 'C' is
the same as if they were not nested in some other class.
Note that neither one of 'B' or 'C' has access to private
parts of 'A' either.
The Solaris 4.2 compiler says its a warning that C cannot access private
class B

That's correct. I'd see it as an error rather than a warning though.
(even though its a sibling).


C++ has no concept of 'sibling'.
Both the aCC (HP-UX) and the IBM (AIX)
compiler fail on this.

Good compilers.
I'm looking at page 160 of the ANSC++ spec (ISO/IEC
14882:1998(E) ) about nested classes but don't see any reference to scoping
of them.

What you're asking about is not a 'scoping', but
an 'access' issue. Two different things.

I've tried variations on friend but to no success.

What 'variations'?

class A{
private:
class B{
friend class A::C;
};

class C{
MyTemplate<B> foo;

};
};

-Mike
 
J

John Harrison

I've tried variations on friend but to no success.
What 'variations'?

class A{
private:
class B{
friend class A::C;
};

class C{
MyTemplate<B> foo;

};
};

-Mike

I think you need a forward declaration as well

class A{
private:
class C;
class B{
friend class A::C;
};

class C{
MyTemplate<B> foo;

};
};

john
 

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

Staff online

Members online

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top