Inheriting a Base Enumeration Class

Is there a sample where I can inherit an enum from another enum in C ++ ??

Something like that:

enum eBase { one=1, two, three }; enum eDerived: public eBase { four=4, five, six }; 
+67
c ++ enums design
Mar 13 '09 at 21:03
source share
10 answers

Impossible. No inheritance with transfers.

Instead, you can use classes with named const ints.

Example:

 class Colors { public: static const int RED = 1; static const int GREEN = 2; }; class RGB : public Colors { static const int BLUE = 10; }; class FourColors : public Colors { public: static const int ORANGE = 100; static const int PURPLE = 101; }; 
+55
Mar 13 '09 at 21:05
source share
 #include <iostream> #include <ostream> class Enum { public: enum { One = 1, Two, Last }; }; class EnumDeriv : public Enum { public: enum { Three = Enum::Last, Four, Five }; }; int main() { std::cout << EnumDeriv::One << std::endl; std::cout << EnumDeriv::Four << std::endl; return 0; } 
+83
Mar 13 '09 at 21:11
source share

You cannot do this directly, but you can try using the solution from this article.

The main idea is to use an auxiliary template class that contains enumeration values ​​and has a type conversion operator. Given that the base type for enum is int you can easily use this holder class in your code instead of enum.

+9
Apr 08 '10 at 4:01
source share

Unfortunately, this is not possible in C ++ 14. I hope we have such a language function in C ++ 17. Since you already have several workarounds for your problem, I will not offer a solution.

I would like to point out that the wording should be an “extension” and not an “inheritance”. An extension allows more values ​​(since you go from 3 to 6 values ​​in your example), while inheritance means adding more restrictions to a given base class, so the set of features is compressed. Therefore, potential casting will work exactly the opposite of inheritance. You can distinguish a derived class from a base class, and not vice versa, with class inheritance. But when you have extensions, you should be able to apply the base class to its extension, and not vice versa. I say “must” because, as I said, such a language function still does not exist.

+5
Sep 18 '14 at 21:49
source share

How about this? Ok, an instance is created for every possible value, but it is also very flexible. Are there any disadvantages?

.h:

 class BaseEnum { public: static const BaseEnum ONE; static const BaseEnum TWO; bool operator==(const BaseEnum& other); protected: BaseEnum() : i(maxI++) {} const int i; static int maxI; }; class DerivedEnum : public BaseEnum { public: static const DerivedEnum THREE; }; 

.cpp

 int BaseEnum::maxI = 0; bool BaseEnum::operator==(const BaseEnum& other) { return i == other.i; } const BaseEnum BaseEnum::ONE; const BaseEnum BaseEnum::TWO; const DerivedEnum DerivedEnum::THREE; 

Application:

 BaseEnum e = DerivedEnum::THREE; if (e == DerivedEnum::THREE) { std::cerr << "equal" << std::endl; } 
+3
Oct 26 '14 at 16:47
source share

Well, if you define an enum with the same name in a derived class and start it from the last element of the enum correspondent in the base class, you will get almost what you want - an inherited enumeration. Take a look at this code:

 class Base { public: enum ErrorType { GeneralError, NoMemory, FileNotFound, LastItem } } class Inherited: public Base { enum ErrorType { SocketError = Base::LastItem, NotEnoughBandwidth, } } 
+2
Jun 29 '10 at 5:03 on
source share

As bayda pointed bayda , the enumeration does not have functionality (and / or should not), so I applied the following approach to your predicament by adapting Mykola Golubyev response:

 typedef struct { enum { ONE = 1, TWO, LAST }; }BaseEnum; typedef struct : public BaseEnum { enum { THREE = BaseEnum::LAST, FOUR, FIVE }; }DerivedEnum; 
+2
Mar 06 '13 at 1:46 on
source share

You can use the SuperEnum project to create extensible enumerations.

 /*** my_enum.h ***/ class MyEnum: public SuperEnum<MyEnum> { public: MyEnum() {} explicit MyEnum(const int &value): SuperEnum(value) {} static const MyEnum element1; static const MyEnum element2; static const MyEnum element3; }; /*** my_enum.cpp ***/ const MyEnum MyEnum::element1(1); const MyEnum MyEnum::element2; const MyEnum MyEnum::element3; /*** my_enum2.h ***/ class MyEnum2: public MyEnum { public: MyEnum2() {} explicit MyEnum2(const int &value): MyEnum(value) {} static const MyEnum2 element4; static const MyEnum2 element5; }; /*** my_enum2.cpp ***/ const MyEnum2 MyEnum2::element4; const MyEnum2 MyEnum2::element5; /*** main.cpp ***/ std::cout << MyEnum2::element3; // Output: 3 
+1
Jan 05 '18 at 11:56
source share

impossible.
But you can define an anonymous enumeration in a class, and then add additional enumeration constants to the derived classes.

0
Mar 13 '09 at 21:18
source share
 enum xx { ONE = 1, TWO, xx_Done }; enum yy { THREE = xx_Done, FOUR, }; typedef int myenum; static map<myenum,string>& mymap() { static map<myenum,string> statmap; statmap[ONE] = "One"; statmap[TWO] = "Two"; statmap[THREE] = "Three"; statmap[FOUR] = "Four"; return statmap; } 

Using:

 std::string s1 = mamap()[ONE]; std::string s4 = mymap()[FOUR]; 
-one
Jan 10 '19 at 22:30
source share



All Articles