473,238 Members | 1,796 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,238 software developers and data experts.

Concept of nested classes

Hi,

Nested classes only seem to be able to access static members of the
surrounding class :
class SurroundingClass
{
public:
class InnerClass
{
public:
void setSurroundingVariable()
{
SurroundingClass::variable = 4;
}
};
friend class InnerClass;

static int variable;
// int variable; --> ERROR : SurroundClass is not a base class
for type InnerClass
};

I would like to be able to access *instance* members of the
SurroundingClass, rather than only *static* members from within the
InnerClass.

I don't really understand why this is not working.
Perhaps I don't understand the concept of nested classes...

Anyone able to explain, and provide a solution for this problem.

Thanks in advance !

Erik Bongers.
Jul 22 '05 #1
3 2377
"Erik Bongers" <Bo**********@hotmail.com> wrote...
Nested classes only seem to be able to access static members of the
surrounding class :
class SurroundingClass
{
public:
class InnerClass
{
public:
void setSurroundingVariable()
{
SurroundingClass::variable = 4;
}
};
friend class InnerClass;

static int variable;
// int variable; --> ERROR : SurroundClass is not a base class
for type InnerClass
};

I would like to be able to access *instance* members of the
SurroundingClass, rather than only *static* members from within the
InnerClass.

I don't really understand why this is not working.
Perhaps I don't understand the concept of nested classes...
You probably don't. The big difference with, for example, Java's
"nested classes" is that in C++ an object of the surrounding class
_does_not_ by default contain an object of the nested class.
Anyone able to explain, and provide a solution for this problem.


Solution to what problem?

If you need a nested class to be a _data_ member of the surrounding
class, in addition to being a _type_ member, you need to say so:

class Surrounding {
class Nested {
double whatever;
public:
void somefunction();
};

Nested nested_data;
int some_other_data;
};

Now, 'nested_data' is _contained_ in an object of type Surrounding.
To further develop this, to give it access to it's "parent"s other
data members, you need to provide some mechanism, for example, the
'nested_data' member could be constructed to know its "parent":

class Surrounding {
class Nested {
Surrounding& papa;
double whatever;
public:
Nested(Surrounding&);
void somefunction();
};

Nested nested_data;

public:
Surrounding() : nested_data(*this) {}
};

Now, 'nested_data' contains a reference to the "parent" object,
which could be used to access other members of it.

I guess you just need to learn more C++ (and UNlearn some Java
while doing that).

Victor
Jul 22 '05 #2
I thank you for an impressively fast and clear reply.
And you guessed it right : I was hoping to get Java-like functionality here,
to implement what is often called Listeners, Connectors or Observers.
But allow me not to enter the C++ vs. Java debate, as I feel sympathy for
both languages and
I'd rather spend the next hour, implementing your solution...
....for which I thank you again !

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:7i5Ab.431048$HS4.3399688@attbi_s01...
"Erik Bongers" <Bo**********@hotmail.com> wrote...
Nested classes only seem to be able to access static members of the
surrounding class :
class SurroundingClass
{
public:
class InnerClass
{
public:
void setSurroundingVariable()
{
SurroundingClass::variable = 4;
}
};
friend class InnerClass;

static int variable;
// int variable; --> ERROR : SurroundClass is not a base class for type InnerClass
};

I would like to be able to access *instance* members of the
SurroundingClass, rather than only *static* members from within the
InnerClass.

I don't really understand why this is not working.
Perhaps I don't understand the concept of nested classes...


You probably don't. The big difference with, for example, Java's
"nested classes" is that in C++ an object of the surrounding class
_does_not_ by default contain an object of the nested class.
Anyone able to explain, and provide a solution for this problem.


Solution to what problem?

If you need a nested class to be a _data_ member of the surrounding
class, in addition to being a _type_ member, you need to say so:

class Surrounding {
class Nested {
double whatever;
public:
void somefunction();
};

Nested nested_data;
int some_other_data;
};

Now, 'nested_data' is _contained_ in an object of type Surrounding.
To further develop this, to give it access to it's "parent"s other
data members, you need to provide some mechanism, for example, the
'nested_data' member could be constructed to know its "parent":

class Surrounding {
class Nested {
Surrounding& papa;
double whatever;
public:
Nested(Surrounding&);
void somefunction();
};

Nested nested_data;

public:
Surrounding() : nested_data(*this) {}
};

Now, 'nested_data' contains a reference to the "parent" object,
which could be used to access other members of it.

I guess you just need to learn more C++ (and UNlearn some Java
while doing that).

Victor

Jul 22 '05 #3
Erik Bongers wrote:
class SurroundingClass
{
public:
class InnerClass
{
public:
void setSurroundingVariable()
{
SurroundingClass::variable = 4;
What this line says is, "I'm an InnerClass object, but I'm going to
view myself as an instance of SurroundingClass. From that perspective
I have a member 'variable' that I set to 4." But InnerClass is not
actually derived from SurroundingClass, and that is what the compiler
tells you.
Martin
}
};
friend class InnerClass;

static int variable;
// int variable; --> ERROR : SurroundClass is not a
base class for type InnerClass
};

Jul 22 '05 #4

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

Similar topics

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...
4
by: Christopher Ireland | last post by:
Hi -- I'm trying to find an example of a nested class implemented within the .NET Framework itself but without much success. I don't suppose that anybody knows of such an example off the top of...
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...
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.