473,797 Members | 2,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Privacy and scope and love

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
5 1779
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

"Tom Plunket" <to***@fancy.or g> wrote in message
news:ri******** *************** *********@4ax.c om...
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2745
by: Chris Hohmann | last post by:
Does anyone know of a way to enumerate the local variable scope? The reason I ask is that I have a function that dumps the contents of response,request,server,etc... for debugging purposes. I'd like to also display the local variables. TIA -Chris
12
3420
by: Keith Chadwick | last post by:
I have a fairly hefty XSLT file that for the sake of debugging and clarity I wish to split into some separate sub-templates contained within the same file. The master template calls an apply-templates and passes a node set to it. This template in turn defines approximately 15 variables that dictate how the following template should proceed. Pseudo example: <!-- Root transformation called via .NET XSLTransform()--> <xsl:template...
5
2005
by: Information | last post by:
Dear Friends, In VB.NET you can declare something like following.. Module modVar2 Public sb as String End Module Now, sb is a project level scope variable and can be accessed any where in the project.
10
1831
by: Hermit Dave | last post by:
Hi, Depending upon their security settings some users can not login due to their machine's Privacy Settings some how blocking the cookie (no privacy policy available)... which is encrypted... what i would like is someone to tell me how to set privacy policy for the cookie.. i know its probably not related to asp.net but googling didnt help much.. even on msdn wasnt helpful much... maybe i was searching with the wrong keywords... any...
6
3193
by: Zytan | last post by:
I ran through the VB Guided Tour some time ago. In particular, the "Managing Your Records: Using Data in Your Program" section: http://msdn2.microsoft.com/en-us/library/t25kbx0s(VS.80).aspx This explains how to create a SQL database, which is stored as an .mdf file: http://msdn2.microsoft.com/en-us/library/ms172599(VS.80).aspx It is just 4 or 5 addresses being stored. However, the database is 2,240 KB in size! To store 5 addresses? ...
6
2046
by: enaeher | last post by:
I would expect this code: globalFnArray = ; for (var i = 0; i < 5; i++) { globalFnArray.push (function () { alert (i) }); } for (var j = 0; j < 5; j++) { globalFnArray(); } to alert 0, 1, 2, 3, and 4. Instead, I get 5, 5, 5, 5, and 5 in Firefox, indicating that all of
7
2363
by: PJ6 | last post by:
(VS2003) ? int.Parse("2.0") error: identifier 'int' out of scope ? "foo".IndexOf("o") error: expression '"foo".IndexOf' out of scope For the love of god, why is int and other basic stuff out of scope in my Command window when I'm debugging?
8
2157
bilibytes
by: bilibytes | last post by:
Hi everyone, I'm facing a database design problem. I want to make a sort of networking solution for the clients of my site in which they would be able to share or keep private some of their contact information. so if i have a table like this: CREATE TABLE `users_retailers_account_info` ( `id` bigint(15) unsigned NOT NULL auto_increment, `email` varchar(255) NOT NULL default 'N/A',
0
10468
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.