473,785 Members | 2,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enums clash

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
9 1767

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
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_atla s_DOT_de
Oct 31 '05 #3
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
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:0D******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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
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(nam e) \
\
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(Tes t)
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
>
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
* 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

"Jonathan Mcdougall" <jo************ ***@gmail.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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(nam e) \
\
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(Tes t)
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
9556
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 change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
2
2082
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 seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the Enums 'Car' and 'House Public Class Fo Public Enum Ca For Chevrole Toyot
27
2747
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 <typename Enum> class smart_enum { public: typedef Enum enum_type;
4
5593
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 when looking at the db we can't tell what the value in a field represents. The other problem is that our enums are currently all stored in a single class, which means that because of no visibility constraints the enums are often used out of context...
2
2252
by: Deepa K | last post by:
Hi, In my PC, eth0 is not up because of IP address clash (another PC is using the same IP address). I tried to execute a file which has set of insert commands. After executing the file, when I go and see the database, no rows are inserted. I redirected the output to another file. It shows success for all inserts, but no rows in database. This is happening only when the PC has IP clash, that too not all times. I wasn't able to trace out...
1
3457
by: David Gaudine | last post by:
(This is a bit like the recent thread "PHP Switching Sessions".) I use session_start(). When I open my web-based application in two windows on the same system, there's a definite clash; I can't do two independent sessions because the session variables are shared. I solved that the easy way, by not opening two windows. But then I went on to write more applications, and found that if I use the same variable names (for session variables)...
2
2884
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, g++ seems to create enums of 4 bytes in length, unless I use the -fshort-enums option. I don't much want to use this all or nothing approach in case it breaks library code etc, and there's no guarantee that other compilers have a comparable...
11
12559
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, I can cast a MyEnum instance down to the int / short whatever (since int implements IEquatable<int>), but I don't like doing that, as it feels a bit messy, and I am then propegating the things that know what the base represenation is...
7
5265
by: Kurt | last post by:
Hi. I have a c++ project that is basically a set of implementation hiding classes, so that we can convert a static lib with a lot of dependancies into a dynamic lib with less dependancies, so that users of the lib dont require all the development tools we used to develop the lib. I have noticed something strange, and was wondering if its a C++ feature, or a implementation specific bug. I have a function like this:
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10151
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
10092
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
9950
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...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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
5381
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
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.