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

enums clash

P: n/a
Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;
If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'
How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.

JMu
Oct 31 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Jarmo Muukka wrote:
Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;
If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'
How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.

JMu


Well, generally speaking you should write in the language you're using,
not the language you've used elsewhere. Anyway, you can something like
what you want like this:

namespace Answer { enum Enum { Wrong, Right }; }
namespace Lane { enum Enum { Right, Left }; }

Answer::Enum ans = Answer::Right;
Lane::Enum lane = Lane::Right;

Cheers! --M

Oct 31 '05 #2

P: n/a
Jarmo Muukka wrote:
Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;
If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'
How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.


C++ enum values are part of the surrounding namespace.
What about something like this:

//----------------------
namespace Lane
{
enum t { Left, Right };
}

namespace Answer
{
enum t { Right, Wrong};
}

int main()
{
Lane::t lane = Lane::Right;
Answer::t ans = Answer::Right;

// lane = ans; Won't work!!

return 0;
}

//----------------------
HTH

Stefan
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Oct 31 '05 #3

P: n/a
Jarmo Muukka wrote:
I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;
If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'
How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.


Start with

struct Lane { enum value { Left, Right }; };
struct Answer { enum value { Right, Wrong }; };

What you don't immediately have here is conversion to 'int'. And your
syntax becomes somewhat more convoluted

Lane::value lane = Lane::Right;

But you definitely acquire type safety.

V
Oct 31 '05 #4

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:0D*******************@newsread1.mlpsca01.us.t o.verio.net...
Jarmo Muukka wrote:
I have used C++ for many years and recently I have used C#. Now I am coding in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;
If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of 'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'
How would you fix these problems, if you would want to write it in C# way? I would like to use logical names. It's not always possible to know what other enums there are defined or will be defined.


Start with

struct Lane { enum value { Left, Right }; };
struct Answer { enum value { Right, Wrong }; };

What you don't immediately have here is conversion to 'int'. And your
syntax becomes somewhat more convoluted

Lane::value lane = Lane::Right;

But you definitely acquire type safety.

V


Thank you mlimber, Stefan and Victor. I already have tried namespaces and
classes as typename. In my test I named my enums as Value (as Victor did). I
thought that if I have forgotten something and there would be better way,
but it seems that there isn't, so here is the code that I will use as base
technique from now on:

struct Lane
{
enum value
{
Left,
Right
};
};

struct Answer
{
enum value
{
Right,
Wrong
};
};

int _tmain( int argc, _TCHAR* argv[] )
{
Lane::value lane = Lane::Right;
Answer::value answer = Answer::Right;

// lane = answer;

return 0;
}

I am not 100% convinced that will I use namespace or struct, but this is
just a minor change in the definition, if I decide otherwise.

Thanks,
JMu
Oct 31 '05 #5

P: n/a
Jarmo Muukka wrote:
Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;
If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'
How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.


# define scoped_enum(name) \
\
class name \
{ \
public: \
enum E \
{

# define scoped_enum_end(name) \
\
}; \
private: \
E e_; \
\
public: \
name() \
{ \
} \
\
name(E e) \
:e_(e) \
{ \
} \
\
name(int i) \
:e_(E(i)) \
{ \
} \
\
name operator|=(name t2) \
{ \
e_ = E( e_ | t2.e_ ); \
return *this; \
} \
\
name operator&=(name t2) \
{ \
e_ = E( e_ & t2.e_ ); \
return *this; \
} \
\
name operator^=(name t2) \
{ \
e_ = E( e_ ^ t2.e_ ); \
return *this; \
} \
\
operator int() \
{ \
return e_; \
} \
\
public: \
\
\
\
};

scoped_enum(Test)
one, two
scoped_enum_end(Test)

int main()
{
Test t1(Test::one);
Test t2(t1);

t1 |= Test::one;

if ( t1 & Test::one )
;

switch (t1)
{
case Test::one:
{
break;
}
}

if (t1 == Test::one)
;
}

It works pretty well, I think.
Jonathan

Oct 31 '05 #6

P: n/a
>
I am not 100% convinced that will I use namespace or struct, but this is
just a minor change in the definition, if I decide otherwise.


Not really. The main difference here between namespace and struct is
that you can add to a namespace later on, whereas a struct can only be
created once. So

namespace Lane
{
enum { Left, Right };
}

namespace Lane
{
enum { Inner, Outer };
}

is perfectly legal, but the same code with struct would not be. I
recommend that you use namespace.

john
Oct 31 '05 #7

P: n/a
* John Harrison:

I am not 100% convinced that will I use namespace or struct, but this is
just a minor change in the definition, if I decide otherwise.


Not really. The main difference here between namespace and struct is
that you can add to a namespace later on, whereas a struct can only be
created once. So

namespace Lane
{
enum { Left, Right };
}

namespace Lane
{
enum { Inner, Outer };
}

is perfectly legal, but the same code with struct would not be. I
recommend that you use namespace.


Well, I'd consider that a reason why one should _not_ use a namespace
for wrapping an enum. ;-)

The main difference between class and namespace is, however, that a
class is a type that can be handled by template code.

E.g., it can be passed to enum-agnostic template code that infers min
and max values and whether a given template supports the ++ operator.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 31 '05 #8

P: n/a

"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Jarmo Muukka wrote:
Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;
If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of 'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'
How would you fix these problems, if you would want to write it in C# way? I would like to use logical names. It's not always possible to know what other enums there are defined or will be defined.


# define scoped_enum(name) \
\
class name \
{ \
public: \
enum E \
{

# define scoped_enum_end(name) \
\
}; \
private: \
E e_; \
\
public: \
name() \
{ \
} \
\
name(E e) \
:e_(e) \
{ \
} \
\
name(int i) \
:e_(E(i)) \
{ \
} \
\
name operator|=(name t2) \
{ \
e_ = E( e_ | t2.e_ ); \
return *this; \
} \
\
name operator&=(name t2) \
{ \
e_ = E( e_ & t2.e_ ); \
return *this; \
} \
\
name operator^=(name t2) \
{ \
e_ = E( e_ ^ t2.e_ ); \
return *this; \
} \
\
operator int() \
{ \
return e_; \
} \
\
public: \
\
\
\
};

scoped_enum(Test)
one, two
scoped_enum_end(Test)

int main()
{
Test t1(Test::one);
Test t2(t1);

t1 |= Test::one;

if ( t1 & Test::one )
;

switch (t1)
{
case Test::one:
{
break;
}
}

if (t1 == Test::one)
;
}

It works pretty well, I think.
Jonathan


Hello Jonathan,

Thanks for the tip. I modified my code to look like following (the actual
implementation has other types of course):

class Answer
{
public:
enum Code
{
Left,
Right
};

Answer( Code code )
: value( code )
{
}
operator int() const
{
return this->value;
}
private:
Code value;
};

With this implementation I can write:
Answer answer = Answer::Right;
Lane lane = Lane::Right;

The enum is a bit long compared to standard enum, but its usage looks good
to me.

Thanks again,
Jarmo
Oct 31 '05 #9

P: n/a
"Alf P. Steinbach" <al***@start.no> wrote in message
news:43*****************@news.individual.net...
* John Harrison:

I am not 100% convinced that will I use namespace or struct, but this is just a minor change in the definition, if I decide otherwise.


Not really. The main difference here between namespace and struct is
that you can add to a namespace later on, whereas a struct can only be
created once. So

namespace Lane
{
enum { Left, Right };
}

namespace Lane
{
enum { Inner, Outer };
}

is perfectly legal, but the same code with struct would not be. I
recommend that you use namespace.


Well, I'd consider that a reason why one should _not_ use a namespace
for wrapping an enum. ;-)

The main difference between class and namespace is, however, that a
class is a type that can be handled by template code.

E.g., it can be passed to enum-agnostic template code that infers min
and max values and whether a given template supports the ++ operator.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Hi,

I have dropped the idea of using namespace for wrapping an enum. The enums I
try to wrap can be wrapped with a class and it works nicely and the code
looks logical to me. See my other answer.

Thanks,
Jarmo
Nov 1 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.