Topic: enum, Why does C++ still keep the ugly C feature
Author: hyrosen@mail.com (Hyman Rosen)
Date: Thu, 6 Apr 2006 14:50:11 GMT Raw View
cnjinhao@hotmail.com wrote:
> 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 ]
Author: cnjinhao@hotmail.com
Date: Thu, 6 Apr 2006 23:33:50 CST Raw View
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 ]
Author: musiphil@bawi.org (Seungbeom Kim)
Date: Fri, 7 Apr 2006 23:33:20 GMT Raw View
cnjinhao@hotmail.com wrote:
> 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 ]
Author: "SuperKoko" <tabkannaz@yahoo.fr>
Date: Fri, 14 Apr 2006 02:33:29 CST Raw View
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 ]
Author: cnjinhao@hotmail.com
Date: 6 Apr 2006 06:20:02 GMT Raw View
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 ]
Author: Zara <yozara@terra.es>
Date: 6 Apr 2006 14:50:02 GMT Raw View
On 6 Apr 2006 06:20:02 GMT, cnjinhao@hotmail.com wrote:
>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 ]
Author: ramagnus@t-online.de (Rolf Magnus)
Date: Thu, 6 Apr 2006 14:47:16 GMT Raw View
cnjinhao@hotmail.com wrote:
> 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 ]