March 01, 2010

C++ enum inheritance

Today I needed to have enums with an inheritance ability. I have some HMI panels in an application where each panel button has a handler function, which is placed incide a stl vector. Each of these buttons can be enabled and disabled using a unique ID, it's number in the vector.

The problem I fased was that the base class implementing the basic functionality of the panels (done and cancel) also implemented the indecies for these functions. Now in a derived class i wanted to extend the enum with button indecies for the buttons added by the new class, but enums and C++ does not support this directly.

To overcome the problem a template can be used. here is a similar approach using atemplate to extend enum definitions. This solved my problems and I can get the panels back in action.

No comments: