0

I have read a number of posts (i.e. Base enum class inheritance) that show ways to trick the compiler into extending a derived class such that the enumerations continue.

The problem I see with this approach is that should you need a new enumeration in the base class, this throws off the numbering system for the derived class. So long as you don't need to read in saved files, this is probably ok.

But, what if the class structure is for a serialized file? To me, this seems like a problem. What is the suggested best practice for dealing with this situation?

Community
  • 1
  • 1
user3072517
  • 513
  • 1
  • 7
  • 21

1 Answers1

0

Just give your values two parts: which class they belong to, and the enumerated value. Like this:

struct B
{
  enum X { b0, b1, b2, b3 };
};

struct C : B
{
  enum Y { c0, c1 };
};

enum WhichClass { b, c };

Now you can store a pair: the first element is WhichClass and the second is either X or Y.

Another idea would be to simply require each class to have a range of values, e.g. the first class gets 0 to 999, second class gets 1000 to 1999, etc. You can implement this similarly to the post you linked, but using an explicit, higher value for the base class Last value.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    Interesting idea with the two parameters. For the casual onlooker, it probably makes it a bit more cryptic to follow, but interesting idea. I have thought about the second option you mentioned. But was wondering if there was more elegant way. This certainly works, and is fairly straightforward to pick up on as long as you know the max upper limit of the base class. Thanks John for your creative ideas. – user3072517 Jan 20 '15 at 02:14