Inner classes
-
Hello, I am declaring a class within another class. In VC 7.0 the inner class has access to protected members of the enclosing class, but in VC 6.0 it does not. Here is the example:
#include <iostream>
class Big
{
protected:
static const int CONST_VALUE;class Inner
{
public:
Inner ();
int x;
};public:
Inner in;
};const int Big::CONST_VALUE = 2;
Big::Inner::Inner ()
{
x = Big::CONST_VALUE; /* <- error here in VC 6, cannot access protected member etc...
If I declare Inner as fried of Big it works well */
}int main ()
{
Big big;
std::cout << big.in.x;
}Can anyone tell me if there is a bug in the(se) compiler(s)? Thanks! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
-
Hello, I am declaring a class within another class. In VC 7.0 the inner class has access to protected members of the enclosing class, but in VC 6.0 it does not. Here is the example:
#include <iostream>
class Big
{
protected:
static const int CONST_VALUE;class Inner
{
public:
Inner ();
int x;
};public:
Inner in;
};const int Big::CONST_VALUE = 2;
Big::Inner::Inner ()
{
x = Big::CONST_VALUE; /* <- error here in VC 6, cannot access protected member etc...
If I declare Inner as fried of Big it works well */
}int main ()
{
Big big;
std::cout << big.in.x;
}Can anyone tell me if there is a bug in the(se) compiler(s)? Thanks! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
I can't tell for certain, but it appears to me as a bug in the VC7 compiler. I could be wrong, of course, since VC7 is supposed to be more compliant -- I don't have the ARM with me. But it just seems to me like protected means that it's accessible to derived classes. There's no provision for it being accessible to nested classes. Regards, Alvaro
Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)
-
Hello, I am declaring a class within another class. In VC 7.0 the inner class has access to protected members of the enclosing class, but in VC 6.0 it does not. Here is the example:
#include <iostream>
class Big
{
protected:
static const int CONST_VALUE;class Inner
{
public:
Inner ();
int x;
};public:
Inner in;
};const int Big::CONST_VALUE = 2;
Big::Inner::Inner ()
{
x = Big::CONST_VALUE; /* <- error here in VC 6, cannot access protected member etc...
If I declare Inner as fried of Big it works well */
}int main ()
{
Big big;
std::cout << big.in.x;
}Can anyone tell me if there is a bug in the(se) compiler(s)? Thanks! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
You've declared the
Inner
class as protected. declare it as public and you'll get it to work! Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++! -
Hello, I am declaring a class within another class. In VC 7.0 the inner class has access to protected members of the enclosing class, but in VC 6.0 it does not. Here is the example:
#include <iostream>
class Big
{
protected:
static const int CONST_VALUE;class Inner
{
public:
Inner ();
int x;
};public:
Inner in;
};const int Big::CONST_VALUE = 2;
Big::Inner::Inner ()
{
x = Big::CONST_VALUE; /* <- error here in VC 6, cannot access protected member etc...
If I declare Inner as fried of Big it works well */
}int main ()
{
Big big;
std::cout << big.in.x;
}Can anyone tell me if there is a bug in the(se) compiler(s)? Thanks! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
class Big
{
protected:
static const int CONST_VALUE;
class Inner
{
public:
Inner ();
int x;
};
friend class Inner;
//...
};Add the
friend
declaration, that should do the trick. --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm -
class Big
{
protected:
static const int CONST_VALUE;
class Inner
{
public:
Inner ();
int x;
};
friend class Inner;
//...
};Add the
friend
declaration, that should do the trick. --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_HelmHello, I know that if I declare Inner as friend it works on VC 6.0. My problem is: Which compiler's bug is this? Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
-
Hello, I know that if I declare Inner as friend it works on VC 6.0. My problem is: Which compiler's bug is this? Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
But it is not a compiler bug! Protected members can ONLY be accessed through inheritance. Do it piblic or as Dunn said friend. Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!
-
But it is not a compiler bug! Protected members can ONLY be accessed through inheritance. Do it piblic or as Dunn said friend. Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!
Rickard Andersson wrote: Protected members can ONLY be accessed through inheritance. I understand that perfectly. My problem is why my sample compiles in VC 7.0! :confused: Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
-
Rickard Andersson wrote: Protected members can ONLY be accessed through inheritance. I understand that perfectly. My problem is why my sample compiles in VC 7.0! :confused: Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
Alexandru Savescu wrote: My problem is why my sample compiles in VC 7.0 :wtf::wtf::wtf::wtf::wtf: That's a bug... or something... weird! Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!
-
Hello, I know that if I declare Inner as friend it works on VC 6.0. My problem is: Which compiler's bug is this? Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
Alexandru Savescu wrote: Which compiler's bug is this? That, I don't know. Needing the friend declaration just seemed like The Way It Was Done. You should probably consult the C++ spec for the definitive answer. --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm