473,386 Members | 1,745 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,386 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 2383
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.