473,779 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1894
> 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
2407
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
1204
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 show an example: classes.hpp classes.cpp util.hpp util.cpp main.cpp // classes.hpp namespace Classes { class MyBaseClass {
6
559
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 like MyClass.MyColors = Color.Green or, a 'get', such as Color forecolor = MyClass.MyColors; I want to use an indexer so I can take parameters, such as the color type (e.g. "Foreground", "Background" etc.). With a single member function I couldn't...
8
16920
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. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
2
2047
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, and I am not sure why you would want nested classes anyway. Is there a URL that explains the advantages to nested classes, when they would be used, etc? What are your thoughts? Also, if a Class has nothing before it (i.e. no Public or Private,...
2
2374
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 when a nested class inherits from it’s parent class you get this infinite class chain in intellisense when consuming the class. To get around this I created two child classes Reader and Writer which require a base Person object. When consuming...
5
2284
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
2127
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 decided to use nested class because I realized that it was the most convenient way to implement my need. Since this feature is supported in many languages, I was just surprised that Python did support it only partially, hence my original email. ...
0
143
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 now, even with the pickle limitation (not every class are intended to be pickled), more you didn't give any evidence or any pertinent quote of this and at least one of Guido in the above threads seems contradict you :...
2
2290
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 <stack> template <class T> class A { public:
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.