Extending enum in derived classes

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

    To me, it seems more reasonable to have an exception class for each exception reason. When handling an exception, it is usually not interesting to know which class threw the exception, but for what reason the exception was thrown.

    If you want to keep your design: C++ does not allow you to extend an existing enum, but you can create a new enum that starts where a previous one left off:

    class BaseException : public std::exception
    {
       enum {THIS_REASON, THAT_REASON, END_OF_BASE_REASONS };
    };
    
    class DerivedException : public BaseException
    {
       enum {OTHER_REASON = BaseException::END_OF_BASE_REASONS };
    };
    

提交回复
热议问题