Re: one enum is a subset of another
This is a multi-part message in MIME format.
--------------010206080101030209060304
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
On 02/01/12 02:51, Marcel M?ller wrote:
On 31.01.2012 23:56, Christopher wrote:
A developer before me created a large enumeration we can call
EnumType1.
He then, right under it, typedef-ed another
typedef EnumType1 EnumType2
He then created a comment "alias, should only include x types", where
x is a rule defining a subset of EnumType1
This is crap imo. Any function or method declared to take EnumType2,
would happily take a value from EnumType1 that does not meet the x
criteria.
Exactly. You need different types.
Since I know the subset from EmunType1 that meets the x criteria, how
can I define a subset EnumType2 that only includes those enums without
typing the whole darn subset twice?
There is no language support for this kind of problem.
AFAIK you have two options:
1. repeat the definition of the common constants.
2. declare your own enum classes.
In the latter case you need to know that Citrus is no subclass of Fruit,
because you can safely cast from the subset to the general one but not
the other way around.
class Citrus
{public:
static const Citrus ORANGE;
static const Citrus GRAPEFRUIT;
//...
protected:
Citrus() {}
};
class Fruit : Citrus
{ static const Fruit APPLE;
//...
protected:
Fruit() {}
};
bool operator==(const Citrus& l, const Citrus& r)
{ return &l == &r;
}
bool operator!=(const Citrus& l, const Citrus& r)
{ return &l != &r;
}
Note that this pseudo enums have no value, since the fixed number of
instances is already sufficient for uniqueness. This is similar to Java
2 like enums.
[snip]
Hi Marcel,
I tried a slightly modified form of your code shown in the attached.
However, trying to compile it, without the #define DEF_FRUITS, I got the
error messages:
../build/gcc4_6v/gcc.test/enum_static_const.o: In function `main':
/home/evansl/prog_dev/gcc.test/enum_static_const.cpp:37: undefined
reference to `Fruit::APPLE'
/home/evansl/prog_dev/gcc.test/enum_static_const.cpp:38: undefined
reference to `Citrus::ORANGE'
collect2: ld returned 1 exit status
In addition, I'm guessing you meant the copy CTOR's should be private
too to avoid any other instances of Fruit or Citrus from being
created. Is that right?
-regards,
Larry
--------------010206080101030209060304
Content-Type: text/x-c++src;
name="enum_static_const.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="enum_static_const.cpp"
//Following code base on that posted here:
/*
http://groups.google.com/group/comp.lang.c++/msg/a536ca2243a24e22
*/
//with headings:
/*
Date: Wed, 01 Feb 2012 09:51:03 +0100
Newsgroups: comp.lang.c++
Subject: Re: one enum is a subset of another
*/
class Citrus
{public:
static const Citrus ORANGE;
static const Citrus GRAPEFRUIT;
//...
protected:
Citrus() {}
private:
Citrus(Citrus const&);
void operator=(Citrus const&);
};
#define DEF_FRUITS
#ifdef DEF_FRUITS
const Citrus Citrus::ORANGE;
const Citrus Citrus::GRAPEFRUIT;
#endif
class Fruit : public Citrus
{public:
static const Fruit APPLE;
//...
protected:
Fruit() {}
private:
Fruit(Fruit const&);
void operator=(Fruit const&);
};
#ifdef DEF_FRUITS
const Fruit Fruit::APPLE;
#endif
int
main()
{
//Fruit my_apple(Fruit::APPLE);//should fail to compile since copy CTOR private.
Fruit const& apple=Fruit::APPLE;
Citrus const& orange=Fruit::ORANGE;
return &apple==&orange;
}
--------------010206080101030209060304--