By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,973 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

Concept of nested classes

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.