Archived

This topic is now archived and is closed to further replies.

Vlion

using variables in nested class that belong to super-class

Recommended Posts

Say I have:
class C
{
public:
    int i;
    class N
    {
         f() { i = 0; }
    };
};

VC 2003 informs me that C::i must be static in order to use it in N. How can I get around this?

Share this post


Link to post
Share on other sites
C++ isn''t Java. Inner classes don''t have an implicit reference to the surrounding class. However, they do have access to private members of the surrounding class if they are given a reference to it.

Something like this:


class C
{
public:
class N
{
f( C& c ) { c.i = 0; }
};

private:
int i;
};



--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
Thanks.

Shortly after I posed I noticed the relevant section in a draft of the standard.


/*
Except by using explicit pointers, references, and object names, declarations in a nested class can use only
type names, static members, and enumerators from the enclosing class. [Example:*/

int x;
int y;
class enclose {
public:
int x;
static int s;
class inner {
void f(int i)
{
int a = sizeof(x); // error: refers to enclose::x

x = i; // error: assign to enclose::x

s = i; // ok: assign to enclose::s

::x = i; // ok: assign to global x

y = i; // ok: assign to global y

}
void g(enclose* p, int i)
{
p>
x = i; // ok: assign to enclose::x

}
};
};
inner* p = 0; // error ‘inner’ not in scope

//—end example]

Share this post


Link to post
Share on other sites
I think you also have to declare the inner class as friend of the outer class, like this:


class C
{
friend class N;
private:
int i;
public:
class N
{
private:
C* parent;
public:
N(C* p) : parent(p) { } // be careful with the parent pointer here, it points to an object not yet fully constructed

f() { parent->i = 0; }
} inst;

C() : inst(this) { }
};

Share this post


Link to post
Share on other sites
According to VC2003(*cough*), you don''t.
I''d have to run it by gcc 3.3.3 to say with more assurance.

Share this post


Link to post
Share on other sites
quote:
Original post by fallenang3l
I think you also have to declare the inner class as friend of the outer class, like this:



Considering C::i is a public variable, friendship is not necessary for any class.

edit: That is in the original post, C::I is a public variable, so friendship is not necessary for any class.

[edited by - SiCrane on April 4, 2004 11:09:42 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Vlion
According to VC2003(*cough*), you don''t.
I''d have to run it by gcc 3.3.3 to say with more assurance.


According to boost.org''s compiler compatability tests, gcc 3.3.3 and vc2003 have the exact same pass rate. While it''s certainly not a 1:1 mapping of standards compliance, it does seem to suggest that your gcc-fanboy attitude is unwarranted.

Share this post


Link to post
Share on other sites
if i understood the chart right, vc7.1 is actually better. it says gcc 3.3.3 fails 4 times, vc7.1 only fails 1. percent wise, they are the same though.

Share this post


Link to post
Share on other sites