C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

enum, Why does C++ still keep the ugly C feature

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Apr 06, 2006 7:06 am    Post subject: enum, Why does C++ still keep the ugly C feature Reply with 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.

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





PostPosted: Thu Apr 06, 2006 3:06 pm    Post subject: Re: enum, Why does C++ still keep the ugly C feature Reply with quote



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





PostPosted: Thu Apr 06, 2006 3:06 pm    Post subject: Re: enum, Why does C++ still keep the ugly C feature Reply with quote



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





PostPosted: Thu Apr 06, 2006 3:06 pm    Post subject: Re: enum, Why does C++ still keep the ugly C feature Reply with quote

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






PostPosted: Fri Apr 07, 2006 5:06 am    Post subject: Re: enum, Why does C++ still keep the ugly C feature Reply with 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

---
[ 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





PostPosted: Sat Apr 08, 2006 2:06 am    Post subject: Re: enum, Why does C++ still keep the ugly C feature Reply with quote

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





PostPosted: Fri Apr 14, 2006 8:06 am    Post subject: Re: enum, Why does C++ still keep the ugly C feature Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.