Inner classes and access restrictions

C

Carlos Martinez

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

Victor Bazarov

Carlos said:
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
 
T

Tom Widmer

Carlos said:
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/wg21/docs/cwg_defects.html#45
Currently, the workaround using "friend" is actualy illegal. See:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.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
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top