"Bertel Brander" <bertel@post4.tele.dk> wrote:
[8<8<8<]
> Men hvorfor har de vise fædre til C++ bestemt at man ikke må det?
På grund af at _pointere_ til 2 forskelle enum er forskellige type i C, og
det vil give problemer med funktions overload hvis ikke også hver enum blev
gjort til en separat type.
Det er beskrevet i
The Design and Evolution of C++
Bjarne Stroustrup
ISBN 0-201-54330-3
side 253-254.
Der bliver så vidt jeg kan se arbejdet på at forbedre enum i næste version
af C++.
Se f.eks.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1579.pdf
[8<8<8<]
> Men det vil Borland og MS VC++ ikke oversætte. Min evner rækker
> ikke til at lave noget der både kan oversættes og virker
>
> Så jeg må vist leve med:
> for(Day = Monday; Day < Numdays; Day = WeekDay(int(Day) + 1))
Du kan lave en type der gør hvad du ønsker.
Følgende oversætter både med Microsoft Visual C++ .NET 2003 og Borland
C++Builder 6
<C++ kode>
#include <iostream>
class week_day
{
public:
static const week_day monday, tuesday, wednesday,
thursday, friday,
saturday, sunday, end;
static const week_day& begin;
week_day(const week_day& week_day_arg) :
value(week_day_arg.value) {}
bool operator==(const week_day& week_day_arg) const
{ return week_day_arg.value == value; }
bool operator!=(const week_day& week_day_arg) const
{ return !operator==(week_day_arg); }
week_day& operator++()
{
if(end_ != value)
value = week_day_(value+1);
return *this;
}
private:
friend std::ostream& operator<<(std::ostream&, const week_day&);
enum week_day_ {
monday_, tuesday_, wednesday_,
thursday_, friday_,
saturday_, sunday_, end_
} value;
week_day(week_day_ value_arg) :
value(value_arg) {}
};
std::ostream& operator<<(std::ostream& os, const week_day& wd)
{
static const char* week_day_text[] =
{ "monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday", "end"
};
os << week_day_text[wd.value];
return os;
}
const week_day week_day::monday(week_day::monday_),
week_day::tuesday(week_day::tuesday_),
week_day::wednesday(week_day::wednesday_),
week_day::thursday(week_day::thursday_),
week_day::friday(week_day::friday_),
week_day::saturday(week_day::saturday_),
week_day::sunday(week_day::sunday_),
week_day::end(week_day::end_);
const week_day& week_day::begin = week_day::monday;
int main()
{
using namespace std;
for(week_day wd = week_day::begin; week_day::end != wd; ++wd)
cout << wd << endl;
}
</C++ kode>
Venlig hilsen
Mogens Hansen