Inherit enum members

Adam D. Ruppe destructionator at gmail.com
Sun Apr 21 21:05:46 UTC 2019


On Sunday, 21 April 2019 at 20:58:19 UTC, Andrey wrote:
> I have got 2 enums. How to inherit one enum from another?

You don't. enums don't inherit, but rather have a base type. That 
means each enum member must match that base type and the compiler 
is looser about conversions between them, but they do not inherit 
members.

enum Key { // no base type
    K1,
    K2
}

int a = Key.K1; // not allowed

enum Key : int { // base type if int
    K1 = 0,
    K2 = 1,
}

int a = Key.K1; // allowed, because of same base type


>> enum ExtendedKey : Key

So this would be defining an enum with base type of Key... 
meaning each member of it must be of type Key. But Key's members 
are NOT carried over.


As far as I know, the language does not permit what you want. 
You'll have to copy over any matching members yourself.


More information about the Digitalmars-d-learn mailing list