Privacy and scope and love

T

Tom Plunket

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!
 
J

Jeff

Hi Tom, GCC 3.2 compiles this code as well. I agree with your
(perhaps ignorant) assumption ... MyStruct has the same access to
MyClass's data as Method().
Jeff
 
H

Howard

Tom Plunket said:
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
 
T

Tom Plunket

Howard said:
Tom said:
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.

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.

Makes sense. Thanks.
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.

Yeah, I tried that but it didn't do what I had hoped.
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.)

I don't actually ever need it externally, and it's really an
implementation detail. I would put it in an unnamed namespace in
the implementation file if I could, but the class declaration
depends on it. :(

What I have to do for Codewarrior is:

.. forward declare the struct
.. make it a friend
.. move the struct definition out of function scope

I'd really rather keep the struct in function scope, simply due
to the desire to keep things wrapped in the tightest possible
scope, but oh well, C++ requires nasty things sometimes.


-tom!
 
M

Martin Eisenberg

Tom said:
Howard wrote:
[snip]
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.)

I don't actually ever need it externally, and it's really an
implementation detail. I would put it in an unnamed namespace
in the implementation file if I could, but the class declaration
depends on it. :(

Well, can you live with the enum being protected?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top