 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Apr 06, 2006 7:06 am Post subject: enum, Why does C++ still keep the ugly C feature |
|
|
enum X{a, b, c};
X i = a;
a, b, c and X are in a same scope. why?
and now, I can't do it like this
struct cost_form_Beijing
{
enum airplane{London, Rome, Berlin};
enum train{London = 100, Rome, Berlin};
};
and I don't like fixed it but what I only can do in this way.
struct cost_form_Chongqing
{
struct airplane
{
const static int London = 0;
const static int Rome = 1;
const static int Berlin = 2;
};
struct train
{
const static int London = 100;
const static int Rome = 101;
const static int Berlin = 102;
};
};
in this way, a big problem is the type of train::London is int, not
train...so
I would like this and I think this feature should be introduced into
C++
enum X{a, b, c};
X i = X::a;
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Apr 06, 2006 3:06 pm Post subject: Re: enum, Why does C++ still keep the ugly C feature |
|
|
cnjinhao (AT) hotmail (DOT) com wrote:
| Quote: | and I don't like fixed it but what I only can do in this way.
struct cost_form_Chongqing
{
struct airplane
{
const static int London = 0;
const static int Rome = 1;
const static int Berlin = 2;
};
struct train
{
const static int London = 100;
const static int Rome = 101;
const static int Berlin = 102;
};
};
in this way, a big problem is the type of train::London is int, not
train...so
|
How about this:
struct airplane
{
enum ap
{
London, Rome, Berlin
};
airplane(ap arg) : value_(arg) {}
operator int() { return value_; }
private:
ap value_;
};
int main()
{
airplane a = airplane::London;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Zara Guest
|
Posted: Thu Apr 06, 2006 3:06 pm Post subject: Re: enum, Why does C++ still keep the ugly C feature |
|
|
On 6 Apr 2006 06:20:02 GMT, cnjinhao (AT) hotmail (DOT) com wrote:
| Quote: | enum X{a, b, c};
X i = a;
a, b, c and X are in a same scope. why?
and now, I can't do it like this
struct cost_form_Beijing
{
enum airplane{London, Rome, Berlin};
enum train{London = 100, Rome, Berlin};
};
and I don't like fixed it but what I only can do in this way.
|
<..>
You may separate by concept:
namespace by_train {
enum destination {London,Rome,Berlin};
}
namespace by_plane {
enum destination {London,Rome,Berlin};
}
Now you may refer to a destination specifying also the means, so you
are really refering to the travel itself:
by_train::London
by_plane::London
Which makes both a separation of the underlying concepts and and easy
to spot meaning of the value
Best regards,
Zara
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Thu Apr 06, 2006 3:06 pm Post subject: Re: enum, Why does C++ still keep the ugly C feature |
|
|
cnjinhao (AT) hotmail (DOT) com wrote:
| Quote: | I would like this and I think this feature should be introduced into C++
enum X{a, b, c};
X i = X::a;
|
namespace X { enum e { a, b, c }; }
X::e i = X::a;
or
struct cost_form_Beijing
{
struct airplane { enum e {London, Rome, Berlin}; };
struct train { enum e {London = 100, Rome, Berlin} };
};
A drop more typing, and the enum type is name::e instead
of just name, but it's not that onerous, and it stays
compatible with C.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Apr 07, 2006 5:06 am Post subject: Re: enum, Why does C++ still keep the ugly C feature |
|
|
thanks for your thread
namespace X { enum e { a, b, c }; }
X::e i = X::a;
what i expect is
X::e i = X::e::a;
a, b, c all are in {}, so, they should be in a scope of e.
in my opinion, it is really a ugly feature that elements of a enum are
in a same scope of their enum
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Sat Apr 08, 2006 2:06 am Post subject: Re: enum, Why does C++ still keep the ugly C feature |
|
|
cnjinhao (AT) hotmail (DOT) com wrote:
| Quote: | thanks for your thread
namespace X { enum e { a, b, c }; }
X::e i = X::a;
what i expect is
X::e i = X::e::a;
a, b, c all are in {}, so, they should be in a scope of e.
in my opinion, it is really a ugly feature that elements of a enum are
in a same scope of their enum
|
You raised a problem, and Hyman suggested a workaround.
What's the problem with the suggested workaround?
The status quo may not be the most elegant design choice, but
it is what we have now and what lots of existing codes depend on.
You are free to insist on a change of the language definition,
but just don't expect it to be accepted easily, especially when
there's an easy workaround.
--
Seungbeom Kim
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
SuperKoko Guest
|
Posted: Fri Apr 14, 2006 8:06 am Post subject: Re: enum, Why does C++ still keep the ugly C feature |
|
|
Here is a complete solution:
struct cost_form_Beijing
{
struct airplane { enum airplane_t {London, Rome, Berlin}; };
typedef airplane::airplane_t airplane_t;
struct train { enum train_t {London = 100, Rome, Berlin}; };
typedef train::train_t train_t;
};
int main()
{
cost_form_Beijing::airplane_t airp=cost_form_Beijing::airplane::London;
}
The only flaw is the fact that the type of the enumeration is now
airplane_t, while the scope used to access the enumeration items is
airplane.
And compatibility with C is maintained.
Note : a typedef can't be used to access scope of the aliased type.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| 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
|
|