Re: Privacy and scope and love

From: Howard (alicebt_at_hotmail.com)
Date: 11/24/03


Date: 24 Nov 2003 19:04:27 GMT


"Tom Plunket" <tomas@fancy.org> wrote in message
news:rictrvc06lhhcapog844cmfn2n69qd3d29@4ax.com...
> I'm having some scoping issues, relating to the fact that
> different compilers do different things. Who's doing the right
> thing here?
>
> class MyClass
> {
> public:
> void Method();
>
> private:
> enum MyEnum
> {
> e0, e1, e2
> };
> };
>
> void MyClass::Method()
> {
> struct MyStruct
> {
> MyEnum e; // compilation error here.
> };
>
> MyStruct ms;
> ms.e = e0;
> }
>
> int main()
> {
> MyClass mc;
> mc.Method();
> }
>
> Metrowerks Codewarrior tells me that MyStruct doesn't have access
> to MyEnum due to the fact that MyEnum is a private member of
> MyClass. I would (ignorantly, perhaps) assume that since
> MyStruct is declared in MyClass's scope that it would have access
> to MyClass private data, but... That's why I'm asking here.
> MSVC.NET allows this code.
>
> If Codewarrior is correct, is there any way to specify friendship
> of MyStruct? What is the proper way to specify MyStruct's scope?
>
> Finally, which compiler is right, and why?
>
> thanks,
> -tom!

In Appendix C of The C++ Programming Language [Stroustrup,1997], the section
on "Access to Base Classes" states that "The members of a member class have
no special access to members of an enclosing class." I know this is
referring to a class declared as a member of another class, but I would
assume that this also applies to classes declared within the scope of a
class' member function as well.

I suppose that you could make a forward declaration of the MyStruct class
(struct), and then delcare is as a friend...? I know that works in the case
of nesting the MyStruct class in the MyClass declaration. It might also
work in this case.

Or, make the enumeration public! (Why make it private? It's not a variable
that someone's going to mess with accidently, but rather a type that you
*may* end up wanting to use elsewhere.)

-Howard



Relevant Pages

  • Re: problem using self built DLL
    ... called MyClass, AND YOU HAVE SAID NOTHING MORE ABOUT IT! ... declaration, wherein you promise to say something later. ... MyClass has no member function or member ... I thought I would start to add them after I get the DLL ...
    (microsoft.public.vc.mfc)
  • Re: problem using self built DLL
    ... runtime, the compiler still has to know about the class and all its methods, members, its ... called MyClass, AND YOU HAVE SAID NOTHING MORE ABOUT IT! ... declaration, wherein you promise to say something later. ... MyClass has no member function or member ...
    (microsoft.public.vc.mfc)
  • Re: Privacy and scope and love
    ... >> Metrowerks Codewarrior tells me that MyStruct doesn't have access ... >> MyStruct is declared in MyClass's scope that it would have access ... > referring to a class declared as a member of another class, ... > of nesting the MyStruct class in the MyClass declaration. ...
    (comp.lang.cpp)
  • Re: problem using self built DLL
    ... says to the compiler, "I will tell you about this class in detail before you need it". ... which says "This is a class with no member variables and no methods". ... class declaration in the header file. ... called MyClass, AND YOU HAVE SAID NOTHING MORE ABOUT IT! ...
    (microsoft.public.vc.mfc)
  • Re: problem using self built DLL
    ... called MyClass, AND YOU HAVE SAID NOTHING MORE ABOUT IT! ... declaration, wherein you promise to say something later. ... specify the library file in linker. ... MyClass has no member function or member ...
    (microsoft.public.vc.mfc)