Re: Privacy and scope and love
From: Howard (alicebt_at_hotmail.com)
Date: 11/24/03
- Next message: tom_usenet: "Re: [C++] Arrays and endl"
- Previous message: Arne Stockman: "Re: Problems with a string function in C++"
- In reply to: Tom Plunket: "Privacy and scope and love"
- Next in thread: Tom Plunket: "Re: Privacy and scope and love"
- Reply: Tom Plunket: "Re: Privacy and scope and love"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: tom_usenet: "Re: [C++] Arrays and endl"
- Previous message: Arne Stockman: "Re: Problems with a string function in C++"
- In reply to: Tom Plunket: "Privacy and scope and love"
- Next in thread: Tom Plunket: "Re: Privacy and scope and love"
- Reply: Tom Plunket: "Re: Privacy and scope and love"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|