By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

Privacy and scope and love

P: n/a
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!
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a

"Tom Plunket" <to***@fancy.org> wrote in message
news:ri********************************@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


Jul 22 '05 #3

P: n/a
Howard wrote:
Tom Plunket wrote:
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!
Jul 22 '05 #4

P: n/a
Tom Plunket wrote:
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?
Jul 22 '05 #5

P: n/a
Martin Eisenberg wrote:
Tom Plunket wrote:
Or, make the enumeration public!


I don't actually ever need it externally, and it's really an
implementation detail.


Well, can you live with the enum being protected?


Ahh, interesting. Will give that one a try. Thanks.

-tom!
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.