Re: Extend enumeration types?
Marcel M?ller <news.5.maazl@spamgourmet.org> writes:
Hi,
is there any smart solution for something like sub classes of
enumeration types?
Let's say we have
enum A
{ Value1,
Value2,
Value3
};
enum B
{ Value1,
Value2,
Value3,
Value4,
Value5
};
then enum B can hold all values that A allows and a few more. (Of
course, they cannot exist in the same namespace or class because of
the duplicate names.)
An implicit conversion from A to B is also reasonable. OK, one should
not call this inheritance since the conversion is in the other
direction.
This goes against the Lyskoff Substitution Principle in the case of
return values.
You must ensure that a method in a subclass returns only values that
the superclass may return, otherwise you could not substitute a
subclass for the superclass. Therefore the types defined in
subclasses for return values must be subtypes.
On the other hand, for parameters, you must ensure that a subclass
accepts all the values a superclass may accept. Therefore the types
defined in subclasses for parameters must be supertypes.
class A{
public:
typedef enum { v1,v2,v3, v4,v5,v6 } ResultEnumA;
virtual EnumA getIt(){return v5;}
typedef enum { p1,p2,p3 } ParamEnumA;
virtual void setThat(int p);/* p in ParamEnumA */
// We cannot use a specific enum for the parameter p, since
// subclasses are allowed to take additionnal values.
};
class B:public A{
public:
typedef enum { v1=A::v1,v2=A::v2,v3=A::v3 } ResultEnumB;
virtual EnumA getIt(){return v2;}
typedef enum { p1=A::p1,p2=A::p2,p3=A::p3, p4,p5,p6 ParamEnumB;
virtual void setThat(int p); /* p in ParamEnumB */
};
--
__Pascal Bourguignon__