Nested classes

  • Thread starter Matthias =?ISO-8859-1?Q?K=E4ppler?=
  • Start date
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

Hello,

I just wanted to ask, if an outer and a nested class share implementation
details, e.g. in that both need to have access to a member of the nested
class, is it common practice to simply make those members of the nested
class public?

class Outer
{
class Inner
{
int bar; // Outer needs access to bar
};
};

Should I just make 'bar' public? Or is there a better solution to access
data in an inner class? I think in Java there are mechanisms built in the
language to exchange data between an inner and outer class, something like
a second this pointer which points to the inner class (or the other way
around, I cant quite remember).

Thanks in advance,
Matthias
 
J

Jonathan Mcdougall

I just wanted to ask, if an outer and a nested class share implementation
details, e.g. in that both need to have access to a member of the nested
class, is it common practice to simply make those members of the nested
class public?

class Outer
{
class Inner
{
int bar; // Outer needs access to bar
};
};

What's the use of an inner class if the outer needs what's inside the inner?


By making bar public, users can do

int main()
{
// if Inner is public
Outer::Inner k;
k.bar = 10;
}

or

Outer::Outer()
{
Inner k;
k.bar = 10;
}

which is not necessarily a good thing, whether Inner is public or not.
Just use some member functions to fiddle with the member variables.

Outer::Outer()
{
Inner k;
k.set_bar(10);
}



Jonathan
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

Jonathan said:
What's the use of an inner class if the outer needs what's inside the
inner?

The inner class is a whole own thing. It's derived from a totally different
base class for example. Yet they need both access to a std::list.
By making bar public, users can do

No, my inner class is declared in the private section of Outer of course.
Like in the example. The nested class is not even visible to the public,
leave alone accessible. It's an implementation detail of Outer. Only Outer
has to access it.
Just use some member functions to fiddle with the member variables.

Outer::Outer()
{
Inner k;
k.set_bar(10);
}

Outer needs to access data inside Inner. What's am I gaining with declaring
a getter in the nested class? Then I can as well use public members.

- Matthias
 
J

Jonathan Mcdougall

Just use some member functions to fiddle with the member variables.
Outer needs to access data inside Inner. What's am I gaining with declaring
a getter in the nested class?

Access control.
Then I can as well use public members.

Of course.

If I understand the problem correctly, the inner class has some data the
outer class has to access. Making that data part of the global
namespace, in the outer or inner class depends on your design, which you
said nothing about. If the outer class needs the inner's data, either
make it public of use accessors.

Just remember that you'll always need a real object to get the data,
there is no this-like keywords for inner or outer classes.


Jonathan
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

Jonathan said:
Access control.


Of course.

If I understand the problem correctly, the inner class has some data the
outer class has to access. Making that data part of the global
namespace, in the outer or inner class depends on your design, which you
said nothing about. If the outer class needs the inner's data, either
make it public of use accessors.

Just remember that you'll always need a real object to get the data,
there is no this-like keywords for inner or outer classes.


Jonathan

Okay, thanks Jonathan.
 

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
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top