473,511 Members | 16,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enums

I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };

is equivalent to

static const int sun = 0;
static const int mon = 1;

but I'd like not to have these global.

I can do day2::sun and use that but I get a warning

about nonstandard extension. It does work though.

I suppose I can wrap it in a static class and use it... maybe something like

static struct day2
{
static const int sun = 0;
static const int mon = 1;
}
Which seems to work but I get errors about no variables in day2. I suppose I
can fix this in some way but I wondering if there is some other better
method. (using the static constant int every time is a little trouble if its
not necessary)

Any ideas?

Thanks,
Jon
Sep 26 '07 #1
16 2153
Actualy what I'm doing is

class X
{
enum Y
{
};
};

and it seems to work fine.
Sep 26 '07 #2
Jon Slaughter wrote:
I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };
Drop that last comma.
>
but I'd like not to have these global.

I can do day2::sun and use that but I get a warning
You could put the enum in a namespace.

namespace day2
{
enum { sun, mon };
}

--
Ian Collins.
Sep 26 '07 #3

"Alf P. Steinbach" <al***@start.nowrote in message
news:13*************@corp.supernews.com...
>* Jon Slaughter:
>I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };

is equivalent to

static const int sun = 0;
static const int mon = 1;

Well, it isn't. The enum is a distinct type. And its underlying integral
type need not be int.

This is what microsoft said, not me.
>but I'd like not to have these global.

I can do day2::sun and use that but I get a warning
about nonstandard extension. It does work though.

Right, that's non-standard.

>I suppose I can wrap it in a static class

There's no such thing as static class.

>and use it... maybe something like

static struct day2
{
static const int sun = 0;
static const int mon = 1;
}

Missing variable name and missing semicolon. By writing static you force
the compiler to assume that you're declaring a variable. It's OK to
define the struct inline there, but you then need a variable name at the
end, and anyway you need a semicolon.
I left the semicolon off on purpose just to bother you. jesus christ man!
>
>Which seems to work but I get errors about no variables in day2.

How can it seem to work when it doesn't compile?

>I suppose I can fix this in some way but I wondering if there is some
other better method. (using the static constant int every time is a
little trouble if its not necessary)

Any ideas?

You can wrap the enum in a class or in a namespace.

A class has the advantage that it can be inherited.

A namespace has the advantage that you can use "using".

I suppose a namespace is a good idea. I used a class already though since
that worked for my purposes. I suppose the usage syntax is identical though.

Thanks,
Jon
Sep 26 '07 #4

"Ian Collins" <ia******@hotmail.comwrote in message
news:5l************@mid.individual.net...
Jon Slaughter wrote:
>I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };
Drop that last comma.
god damn people!! First off I cut and pasted this directly from microsofts
help so bitch at them about irrelevant(for my problem) stuff.

http://msdn2.microsoft.com/en-us/lib...49(VS.80).aspx

>
>>
but I'd like not to have these global.

I can do day2::sun and use that but I get a warning
You could put the enum in a namespace.

namespace day2
{
enum { sun, mon };
}
Yes, that is a better method I suppose. I wrapped it with a class but I
guess I'll change it to a namescape since it seems better(even though its
probably equivalent).

Thanks,
Jon
Sep 26 '07 #5
On 2007-09-26 16:39:37 -0400, Ian Collins <ia******@hotmail.comsaid:
Jon Slaughter wrote:
>I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };
Drop that last comma.
The comma is allowed, and sometimes useful.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 27 '07 #6
Jon Slaughter wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:5l************@mid.individual.net...
>Jon Slaughter wrote:
>>I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };
Drop that last comma.

god damn people!! First off I cut and pasted this directly from microsofts
help so bitch at them about irrelevant(for my problem) stuff.
If you post something that appears incorrect, some one will point his out.

--
Ian Collins.
Sep 27 '07 #7
Pete Becker wrote:
On 2007-09-26 16:39:37 -0400, Ian Collins <ia******@hotmail.comsaid:
>Jon Slaughter wrote:
>>I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };
Drop that last comma.

The comma is allowed, and sometimes useful.
I didn't think it was, from 7.2 I thought an enumerator-list couldn't
end in a comma, so I just tried to compile the OP's code;

Sun CC says:

Warning: Identifier expected instead of "}".

gcc says:

error: comma at end of enumerator list

Which sums up my interpretation of 7.2.

--
Ian Collins.
Sep 27 '07 #8
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:2007092621345916807-pete@versatilecodingcom...
On 2007-09-26 16:39:37 -0400, Ian Collins <ia******@hotmail.comsaid:
>Jon Slaughter wrote:
>>I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };
Drop that last comma.

The comma is allowed, and sometimes useful.
The extra comma is allowed in C99:

http://groups.google.com/group/comp....f41c6dee5f8e52

no?

Sep 27 '07 #9

"Ian Collins" <ia******@hotmail.comwrote in message
news:5m************@mid.individual.net...
Jon Slaughter wrote:
>"Ian Collins" <ia******@hotmail.comwrote in message
news:5l************@mid.individual.net...
>>Jon Slaughter wrote:
I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };

Drop that last comma.

god damn people!! First off I cut and pasted this directly from
microsofts
help so bitch at them about irrelevant(for my problem) stuff.
If you post something that appears incorrect, some one will point his out.
First off is not necessarily an error.

Second, my question wasn't about a syntax error and the problem has nothing
to do with it. Now if it was a serious issue then I could see the need but
else its just pedantism. I'm sure you have better things to do with your
time than waste it writing such irrelevant things? (of course the 2nd half
of your post was very helpful)

Suppose it was an syntax error. Do you really think it has anything to do
with my original problem? And if it did could it not be easily caught when
the example was tried? This is not some subtle issue that needs
addressing... I know you know that but when you post such things it makes
you seem like you are simple minded are a pedant. Of course maybe you are a
pedant and proud of it? ;)
Sep 27 '07 #10
Jon Slaughter wrote:
Of course maybe you are a pedant and proud of it? ;)
I was attempting to help answer your question (which I did). I won't
make that mistake again.

--
Ian Collins.
Sep 27 '07 #11
Chris Thomasson wrote:
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:2007092621345916807-pete@versatilecodingcom...
>On 2007-09-26 16:39:37 -0400, Ian Collins <ia******@hotmail.comsaid:
>>Jon Slaughter wrote:
I'm declaring an enum and I would like to limit the scope

I know that

enum day2 {sun, mon, };

Drop that last comma.

The comma is allowed, and sometimes useful.

The extra comma is allowed in C99:
Where it is listed as a major change from C95, so probably a change from
C++. I assume the change will carry forward to the next C++ standard?

--
Ian Collins.
Sep 27 '07 #12
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:%k****************@newssvr17.news.prodigy.net ...
I'm declaring an enum and I would like to limit the scope
[...]

There is a possible solution in the form of a macro hack:

http://appcore.home.comcast.net/misc...scope-cpp.html
Can this work within your needs?

Sep 27 '07 #13
Chris Thomasson wrote:
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:%k****************@newssvr17.news.prodigy.net ...
>I'm declaring an enum and I would like to limit the scope
[...]

There is a possible solution in the form of a macro hack:

http://appcore.home.comcast.net/misc...scope-cpp.html
That's better than using a namespace?

--
Ian Collins.
Sep 27 '07 #14
On Sep 27, 3:34 am, Pete Becker <p...@versatilecoding.comwrote:
On 2007-09-26 16:39:37 -0400, Ian Collins <ian-n...@hotmail.comsaid:
Jon Slaughter wrote:
I'm declaring an enum and I would like to limit the scope
I know that
enum day2 {sun, mon, };
Drop that last comma.
The comma is allowed, and sometimes useful.
Not according to ISO 14882:1998. Nor was it allowed in C90. It
is allowed in C99, and in the current draft. I don't know what
the status is in ISO 14882:2003, but it really doesn't matter,
since most compilers haven't even fully implemented C++98 yet.
G++ doesn't allow it, for example (and only allows -std=c++98,
not -std=c++03).

Of course, the fact that C90 didn't allow it was probably an
oversight (copied by C++98); I suspect that many compiler
writers considered it as such, and didn't forbid it. But as
long as some do, you're stuck with dropping it, at least in
portable code.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 27 '07 #15
"Ian Collins" <ia******@hotmail.comwrote in message
news:5m************@mid.individual.net...
Chris Thomasson wrote:
>"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:%k****************@newssvr17.news.prodigy.ne t...
>>I'm declaring an enum and I would like to limit the scope
[...]

There is a possible solution in the form of a macro hack:

http://appcore.home.comcast.net/misc...scope-cpp.html
That's better than using a namespace?
Well, not really; those macros were kind of on the hacky/crazy side...

;^)
Anyway, I think I agree with the "apparent" line of *reasoning I deciphered
from your post. Basically, use the namespace keyword to confine an enum
within its scope. Unfortunately, there could be a problem with this approach
because you can't use a namespace within the scope of a struct/class:

____________________

class/struct {
namespace xxx {} // syntax error.
class/struct yyy {} // okay.
};
____________________
Right? I am not an "expert" in the C++ Standard. Therefore, I am probably
missing something real simple here...


*-- Here is some quick example code that tries to fit the bill:

____________________

namespace foolib {
namespace constant {
namespace config {
enum value_e {
value_a,
value_b
};
}} // namespace constant::config
struct foo {
#undef MYCONST
#define MYCONST foolib::constant
struct constant {
struct state {
enum value_e {
value_a = MYCONST::config::value_a + 1,
value_b = MYCONST::config::value_b + 1
};
};}; // struct constant::state
}; // class/struct foo
namespace constant {
namespace state {
#undef MYCONST
#define MYCONST foolib::foo::constant
enum value_e {
value_a = MYCONST::state::value_a - 1,
value_b = MYCONST::state::value_b - 1
};
}} // namespace constant::state

} // namespace foolib

#include <assert.h>
#include <stdio.h>

int main(void) {
namespace myconst = foolib::constant;

assert(
(myconst::config::value_a ==
myconst::state::value_a) &&

(myconst::config::value_b ==
myconst::state::value_b)
);

using namespace std;

puts("done; hit <enter>");
getchar();

return 0;
}

____________________
Something like that looks better than the contrived macro-based stuff no?

Sep 27 '07 #16

"Ian Collins" <ia******@hotmail.comwrote in message
news:5m************@mid.individual.net...
Jon Slaughter wrote:
>Of course maybe you are a pedant and proud of it? ;)
I was attempting to help answer your question (which I did). I won't
make that mistake again.
No problem.. Do what you wish.
Sep 27 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
9513
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
2054
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
2704
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
2
1779
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
5563
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
1979
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
2
2873
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
12422
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
4
2988
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
13
4975
by: Bob | last post by:
Hi, Can someone explain why you can't declare enums in an interface? The compiler says "interfaces can't declare types" Ignoring the syntax implications it seems to me that you should be able to...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7427
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...
1
7085
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...
0
7512
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...
0
3227
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...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
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 ...
0
449
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...

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.