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 9 1748
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
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
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
"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
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
> 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
* 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?
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |