473,386 Members | 1,708 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.

Nested class

Why this code cannot be compiled (under VC++ 7.0):

class Outer {
public:
class Inner {
public:
Inner() : in_data(0) {}
void action()
{
Outer::out_data++; // illegal reference to non-static
member 'Outer::out_data'
}

int in_data;
};

Outer() : out_data(0) {}
void action()
{
Inner::in_data++; // ittlegal reference to non-static member
'Outer::Inner::in_data'
}

int out_data;
};

Thanks in advance!

Jul 22 '05 #1
7 2638
newbiecpp wrote:
Why this code cannot be compiled (under VC++ 7.0):

class Outer {
public:
class Inner {
public:
Inner() : in_data(0) {}
void action()
{
Outer::out_data++; // illegal reference to non-static
member 'Outer::out_data'
}

int in_data;
};

Outer() : out_data(0) {}
void action()
{
Inner::in_data++; // ittlegal reference to non-static member
'Outer::Inner::in_data'
}

int out_data;
};


It should not compile on any standard C++ compiler.

In C++, an inner class is just a type definition, you need to
specifically instantiate an object.

e.g.
Outer::Inner my_inner_object;

in some funtion
....
++ my_inner_object.in_data;
....

would do what you want.

You can obviously have a member variable in Outer that also instaniates
the Inner class.

e.g.
class Outer {
.....
Inner m_inner;
.....
void func()
{
++ m_inner.in_data;
}
};
Jul 22 '05 #2
newbiecpp wrote:
Why this code cannot be compiled (under VC++ 7.0):

class Outer {
public:
class Inner {
public:
Inner() : in_data(0) {}
void action()
{
Outer::out_data++; // illegal reference to
non-static member 'Outer::out_data'
That's exactly the problem. Outer::out_data is a non-static member, you
need an object to access a non-static member of. So you need an object
of class Outer to increment its member out_data, or you need to declare
that member static, depending on what you actually need.
}

int in_data;
};

Outer() : out_data(0) {}
void action()
{
Inner::in_data++; // ittlegal reference to non-static
member 'Outer::Inner::in_data'
Same as before, Inner::in_data is not a static member, so you need an
object of class Inner to access that member.
}

int out_data;
};

Thanks in advance!


Jul 22 '05 #3
On a slightly different thread, I remember seeing some Microsoft
COM code which played games with nested classes by doing some
pointer arithmetic with "this". My recollection is a
bit fuzzy, but I seem to remember the nested class was able to access
private data in the parent. Is this just pure evil, or is there a standard
compliant way of doing this? It would seem to be a useful mechanism
to break up the functionality of the parent class by using nested classes
each specialized in one particular behavior/functionality.

dave

"newbiecpp" <ne*******@yahoo.com> wrote in message
news:qNLTc.13899$ZY3.6813@trndny08...
Why this code cannot be compiled (under VC++ 7.0):

class Outer {
public:
class Inner {
public:
Inner() : in_data(0) {}
void action()
{
Outer::out_data++; // illegal reference to non-static
member 'Outer::out_data'
}

int in_data;
};

Outer() : out_data(0) {}
void action()
{
Inner::in_data++; // ittlegal reference to non-static member 'Outer::Inner::in_data'
}

int out_data;
};

Thanks in advance!

Jul 22 '05 #4
Dave Townsend wrote:

On a slightly different thread, I remember seeing some Microsoft
COM code which played games with nested classes by doing some
pointer arithmetic with "this". My recollection is a
bit fuzzy, but I seem to remember the nested class was able to access
private data in the parent. Is this just pure evil, or is there a standard
compliant way of doing this? It would seem to be a useful mechanism
to break up the functionality of the parent class by using nested classes
each specialized in one particular behavior/functionality.


I think the OP's thinking was more along the lines of the design:
"objects of nested classes keep a pointer to an object the enclosing
class" (which is the case, e.g., in Java but not in C++).

As regarding access rights of a nested class to members of an enclosing
class, see DR45 (I really welcome it and hope it makes it into the next
revision of the standard):

http://www.open-std.org/jtc1/sc22/wg...efects.html#45

Gcc and EDG (not sure about others) already support the proposed new
rule.

Denis
Jul 22 '05 #5
In article <dM********************@comcast.com>, Dave Townsend
<da********@comcast.net> writes
On a slightly different thread, I remember seeing some Microsoft
COM code which played games with nested classes by doing some
pointer arithmetic with "this". My recollection is a
bit fuzzy, but I seem to remember the nested class was able to access
private data in the parent.
Well that has always been allowed (till the Standard was published) by
declaring the nested class a friend of the enclosing class) The wording
of the Standard (clearly a defect) actually makes that impossible in a
Standard conforming way. The resolution of the defect by an almost
unanimous vote of those present (a very rare occurrence) was that nested
classes should have full access to their enclosing class.
Is this just pure evil,
Is what pure evil?
or is there a standard
compliant way of doing this?
At the moment, no but in practice yes. Either have a compiler that
accepts the relevant friend declaration or one that accepts the defect
resolution (all C++ compilers do one or the other or both)
It would seem to be a useful mechanism
to break up the functionality of the parent class by using nested classes
each specialized in one particular behavior/functionality.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Jul 22 '05 #6
In article <41***************@yahoo.removethis.ca>, Denis Remezov
<fi***************@yahoo.removethis.ca> writes
As regarding access rights of a nested class to members of an enclosing
class, see DR45 (I really welcome it and hope it makes it into the next
revision of the standard):


It will.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Jul 22 '05 #7

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:cf********@dispatch.concentric.net...
| newbiecpp wrote:
| > Why this code cannot be compiled (under VC++ 7.0):
| >
| > class Outer {
| > public:
| > class Inner {
| > public:
| > Inner() : in_data(0) {}
| > void action()
| > {
| > Outer::out_data++; // illegal reference to non-static
| > member 'Outer::out_data'
| > }
| >
| > int in_data;
| > };
| >
| > Outer() : out_data(0) {}
| > void action()
| > {
| > Inner::in_data++; // ittlegal reference to non-static member
| > 'Outer::Inner::in_data'
| > }
| >
| > int out_data;
| > };
|
| It should not compile on any standard C++ compiler.
|
| In C++, an inner class is just a type definition, you need to
| specifically instantiate an object.
|
| e.g.
| Outer::Inner my_inner_object;
|
| in some funtion
| ...
| ++ my_inner_object.in_data;
| ...
|
| would do what you want.
|
| You can obviously have a member variable in Outer that also instaniates
| the Inner class.
|
| e.g.
| class Outer {
| ....
| Inner m_inner;
| ....
| void func()
| {
| ++ m_inner.in_data;
| }
| };

You could also use the label type syntax:

class Outer
{
private:
class Inner {
// ...
} InnerObject;

public:
// ...
};

....and initialise 'InnerObject' in the initialiser
list of 'Outer'.

Cheers.
Chris Val
Jul 22 '05 #8

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:
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
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. ...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
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...
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: 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
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.