Skip to main content
Email Password   helpLost your password?

The Problem

C++ does not have a facility to allow one enum type to be extended by inheritance as it does for classes and structures. Sometimes it is important to have this functionality. Suppose you had:

// in Fruit.h

enum Fruit { Orange, Mango, Banana };
// in eat.h

#include "Fruit.h"

void eat(Fruit fruit);

If you wanted to add another fruit type, you could extend Fruit as follows:

enum Fruit { Orange, Mango, Banana, Apple, Pear };

You could also have another function that handles the extra fruit:

void consume(Fruit fruit);

There are two problems with this approach:

  1. Fruit.h may be a library file that you don't necessarily want to change.
  2. void eat(Fruit) is implemented in some library that you can't change and as a result it might not handle 'Apple' properly but will still compile without errors. Your consume(Fruit) function may know about 'Apple' and handle it properly but library users, even though it is not your intention, may still call eat(Apple) with undefined behaviour.

In summary, the results of calls to eat() and consume() are as follows:

  eat( Orange );      // compiles and behaves as expected 

  consume( Orange );  // compiles and behaves as expected 

  eat( Apple );       // compiles with UNDEFINED BEHAVIOUR

  consume( Apple );   // compiles and behaves as expected

The Solution

InheritEnum solves this problem by allowing you to leave the first enum declaration as is and add another enum declaration with new enum types.

Following on from our example, to handle new fruits as well as the first set of fruits, we will then have:

// in -- MyFruit.h --


#include "Fruit.h" 

#include "InheritEnum.h" 


enum NewFruits { Apple, Pear }; 
typedef InheritEnum< NewFruit, Fruit > MyFruit;

Now our consume() declaration becomes:

void consume(MyFruit myfruit); 

Now, our call summary looks as follows:

eat( Orange );       // compiles and behaves as expected

consume( Orange );   // compiles and behaves as expected

eat( Apple );        // does not compile as eat() does not handle NewFruit

consume( Apple );    // compiles and behaves as expected

The Code

// -- InheritEnum.h


template <typename EnumT, typename BaseEnumT>
class InheritEnum
{
public:
  InheritEnum() {}
  InheritEnum(EnumT e)
    : enum_(e)
  {}

  InheritEnum(BaseEnumT e)
    : baseEnum_(e)
  {}

  explicit InheritEnum( int val )
    : enum_(static_cast<EnumT>(val))
  {}

  operator EnumT() const { return enum_; }
private:
  // Note - the value is declared as a union mainly for as a debugging aid. If 

  // the union is undesired and you have other methods of debugging, change it

  // to either of EnumT and do a cast for the constructor that accepts BaseEnumT.

  union
  { 
    EnumT enum_;
    BaseEnumT baseEnum_;
  };
};

Thank you.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalinheriting enums Pin
Atticus-Atropos
22:34 16 Mar '09  
GeneralRe: inheriting enums Pin
Hugo González Castro
6:28 2 Apr '09  
GeneralWorkaround to extend an enum in a derived class Pin
Theo Buys
1:11 6 Jan '09  
GeneralA different approach Pin
Hugo González Castro
18:46 23 Dec '08  
GeneralLicence Pin
warlock6x3
2:43 15 Dec '06  
GeneralRe: Licence Pin
Lidzhade Fhulu
21:44 17 Dec '06  
GeneralThere could be simpler way of doing it. [modified] Pin
Mr.Prakash
21:42 6 Nov '06  
GeneralRe: There could be simpler way of doing it. Pin
Ivan Kolev
22:15 6 Nov '06  
GeneralRe: There could be simpler way of doing it. Pin
Mr.Prakash
22:23 6 Nov '06  
GeneralRe: There could be simpler way of doing it. Pin
Lidzhade Fhulu
0:56 7 Nov '06  
GeneralRe: There could be simpler way of doing it. Pin
Mr.Prakash
2:30 7 Nov '06  
GeneralLooks good at first sight but ... Pin
Roland Pibinger
0:00 31 Oct '06  
GeneralRe: Looks good at first sight but ... Pin
Lidzhade Fhulu
0:21 31 Oct '06  
GeneralRe: Looks good at first sight but ... Pin
Roland Pibinger
0:51 31 Oct '06  
GeneralRe: Looks good at first sight but ... Pin
Lidzhade Fhulu
1:30 31 Oct '06  
GeneralRe: Looks good at first sight but ... Pin
Roland Pibinger
1:51 31 Oct '06  
GeneralRe: Looks good at first sight but ... Pin
Lidzhade Fhulu
2:01 31 Oct '06  
GeneralRe: Looks good at first sight but ... Pin
Elv
11:31 5 Feb '07  
GeneralCool Pin
maihem
14:16 30 Oct '06  
GeneralRe: Cool Pin
Lidzhade Fhulu
19:14 30 Oct '06  


Last Updated 29 Oct 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009