By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

scoping of nested classes

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Robert M. Gary <in*****@agilent.com> wrote in message
news:10***************@cswreg.cos.agilent.com...
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

Jul 19 '05 #2

P: n/a
> >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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.