Extending enum in derived classes

前端 未结 8 2476
陌清茗
陌清茗 2021-02-13 03:53

I have a class hierarchy, and each class in it has an exception class, derived in a parallel hierarchy, thus...

class Base
{
};

class Derived : public Base
{
};         


        
8条回答
  •  故里飘歌
    2021-02-13 04:36

    I would like, in the DerivedException class, to extend the enumeration type to include a new value THE_OTHER_REASON, so that the DerivedException class could hold any of the three values.

    Just assign the first value of a new enum. This works since you're just using the enum as a way of declaring constants.

    class DerivedException : public BaseException
    {
        enum {THE_OTHER_REASON = THAT_REASON + 1, THE_REALLY_OTHER_REASON, ETC};
    };
    

    The problem is that you can't really expect uniqueness of the enums between derived classes, without defining a "chain" of derived classes (i.e. one starts its enums after another's). However, if you only need uniqueness within a single branch of the exception class tree, it can work fine.

提交回复
热议问题