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

Inner classes and access restrictions

P: n/a
Hi all:

Supose the following classes:

class A {

class InnerA1 {

};

class InnerA2 {
InnerA1 myObject;
};

public:

...
};

One compiler compiles ok and the other says that InnerA1 is not
accessible from InnerA2

Inner classes have the same access restrictions than ordinary classes?

must I put InnerA1 inside a public section for use it from InnerA2?

Thanks in advance.
Apr 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Carlos Martinez wrote:
Supose the following classes:

class A {

class InnerA1 {

};

class InnerA2 {
InnerA1 myObject;
};

public:

...
};

One compiler compiles ok and the other says that InnerA1 is not
accessible from InnerA2

Inner classes have the same access restrictions than ordinary classes?
No. All members have access to all members of the same class. The code
above should compile without any problem. It has been discussed several
times, I believe, in comp.std.c++ or c.l.c++.m. Try digging up archived
threads on Google.
must I put InnerA1 inside a public section for use it from InnerA2?


You shouldn't have to, with a compliant compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 19 '06 #2

P: n/a
Carlos Martinez wrote:
Hi all:

Supose the following classes:

class A {

class InnerA1 {

};

class InnerA2 {
InnerA1 myObject;
};

public:

...
};

One compiler compiles ok and the other says that InnerA1 is not
accessible from InnerA2

Inner classes have the same access restrictions than ordinary classes?

must I put InnerA1 inside a public section for use it from InnerA2?


This is an intended change in the language,
http://www.open-std.org/jtc1/sc22/wg...efects.html#45
Currently, the workaround using "friend" is actualy illegal. See:
http://www.open-std.org/jtc1/sc22/wg...efects.html#77

However, even if this is non-standard, it will probably work:

class A {

class InnerA1 {

};

class InnerA2;
friend class InnerA2;
class InnerA2 {
InnerA1 myObject;
};

public:

...
};

Tom
Apr 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.