 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ulrich Eckhardt Guest
|
Posted: Wed Jan 28, 2004 8:13 pm Post subject: preventing values of enumerations to leak into the surroundi |
|
|
EHLO!
The problem is this:
enum ideas
{ none, few, many };
enum time
{ none, little, lots };
This will not compile because 'none' is used twice. My usual solution to
this problem was to divert this to 'no_ideas' and 'no_time', but that isn't
really satisfying.
I now came up with an alternative, which I'd like to hear your opinions
about.
namespace ideas
{
enum type
{ none, few, many };
}
typedef ideas::type idea;
This means that you have to qualify the constants explicitly like
'ideas::few'. Another change is that you have two new symbols, the
typedef'd enum idea and the namespace ideas with the constants. Ideally,
I'd like to have only one such symbol, but I haven't found a way to achieve
that.
Note: when the enum is nested in a UDT, you have to use a struct instead of
a namespace. Not beautiful, but there seems to be no way around it...
BTW: can anyone tell me the rationale why I can't declare (not define!)
enumerations?
thanks
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sean Kelly Guest
|
Posted: Thu Jan 29, 2004 10:05 am Post subject: Re: preventing values of enumerations to leak into the surro |
|
|
Ulrich Eckhardt wrote:
| Quote: |
The problem is this:
enum ideas
{ none, few, many };
enum time
{ none, little, lots };
This will not compile because 'none' is used twice. My usual solution to
this problem was to divert this to 'no_ideas' and 'no_time', but that isn't
really satisfying.
|
It's a matter of personal style, but I always prefix enumerated values
with something to identify what class they belong to. For ideas it might
be i_none, i_few, i_many, etc. The namespace idea works just as well,
with the benefit that you can make it disappear with a using call. I
wouldn't worry about the duplicate symbol or struct issue if that's the
way you decide to go, that's just an effect of using C++.
Sean
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Anthony Williams Guest
|
Posted: Thu Jan 29, 2004 6:46 pm Post subject: Re: preventing values of enumerations to leak into the surro |
|
|
Ulrich Eckhardt <doomster (AT) knuut (DOT) de> writes:
| Quote: | The problem is this:
enum ideas
{ none, few, many };
enum time
{ none, little, lots };
This will not compile because 'none' is used twice.
I now came up with an alternative, which I'd like to hear your opinions
about.
namespace ideas
{
enum type
{ none, few, many };
}
typedef ideas::type idea;
|
This is what I do in all my code.
| Quote: | BTW: can anyone tell me the rationale why I can't declare (not define!)
enumerations?
|
Because the size of the enumerated type is not known until all the enumerated
values are known (an enumerated type with 3 values might only take one byte,
whereas one with 4 billion values will need at least 4 bytes), so the size &
construction of pointers and references are not known either.
Anthony
--
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Fri Jan 30, 2004 9:37 am Post subject: Re: preventing values of enumerations to leak into the surro |
|
|
[email]doomster (AT) knuut (DOT) de[/email] (Ulrich Eckhardt) wrote (abridged):
| Quote: | namespace ideas
{
enum type
{ none, few, many };
}
typedef ideas::type idea;
|
I do something similar, except I don't bother with the typedef and I
don't pluralise the name.
| Quote: | BTW: can anyone tell me the rationale why I can't declare (not
define!) enumerations?
|
Forward declarations were necessary for structs, to allow for
recursive definitions. This reason does not apply to enums, which
cannot be recursive. The standards committee did not see the other
benefits. This has been discussed many times in this newsgroup
before, so you might want to search back.
Some people suggest using a struct instead of a namespace:
struct idea {
enum type { none, few, many } value;
};
Now you can forward-declare "struct idea;" and use its value member
instead of the raw enum.
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Yechezkel Mett Guest
|
Posted: Thu Feb 19, 2004 10:43 am Post subject: Re: preventing values of enumerations to leak into the surro |
|
|
"Ulrich Eckhardt" <doomster (AT) knuut (DOT) de> wrote:
| Quote: | namespace ideas
{
enum type
{ none, few, many };
}
typedef ideas::type idea;
|
My personal preference is
class Idea
{
public:
enum Type
{
none, few, many
};
Idea() {};
Idea(Type value) : value_(value) {}
operator Type() { return value_; } // optional
private:
Type value_;
};
This way you have complete control over conversions etc.
The operator Type() is to allow use in a switch - if you don't
need this, removing it allows fewer nonsensical operations (such
as arithmetic operation on the enum).
Yechezkel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|