473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nested classes

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
Jul 22 '05 #1
4 1867
> 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
Jul 22 '05 #2
Jonathan Mcdougall wrote:
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
Jul 22 '05 #3
>>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?


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
Jul 22 '05 #4
Jonathan Mcdougall wrote:
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?


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


Okay, thanks Jonathan.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
3
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs,...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
3
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.