Extending enum in derived classes

前端 未结 8 2354
陌清茗
陌清茗 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:16

    You could do this, since you use unnamed enums. I suppose you use integer type to store the reason of the exception.

    class DerivedException : public BaseException
    {
        enum { YET_ANOTHER_REASON = THAT_REASON + 1, EVEN_MORE_REASON};
    };
    

    I wouldn't encode reasons as codes. I would prefer specialized exception classes, so I can catch only the exception types that I can handle at one moment.

提交回复
热议问题