using variables in nested class that belong to super-class

Started by
6 comments, last by Vlion 20 years ago
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?
~V'lionBugle4d
Advertisement
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]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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 onlytype 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::xx = i; // error: assign to enclose::xs = i; // ok: assign to enclose::s::x = i; // ok: assign to global xy = 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]
~V'lionBugle4d
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) { }};
According to VC2003(*cough*), you don''t.
I''d have to run it by gcc 3.3.3 to say with more assurance.
~V'lionBugle4d
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]
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.
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.

This topic is closed to new replies.

Advertisement