ENUM and inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Lurye

    ENUM and inheritance

    Hi!

    I have the following problem:

    Suppose that we have the following class:

    class A;

    class AException {

    public:
    enum ERROR { INTERNAL, USAGE, GENERAL };
    AException(ERRO R e):error(e) {}
    inline ERROR GetError() const { return error; }
    private:
    ERROR error;
    };

    A throws AException in case of some error.

    Now I need to write another class

    class B: public A;

    That have all types of errors A have, and also one more type (e.g.
    PARSE).

    I have two solutions for the problem, but I don't like them.

    First is to define BException with it's own enum, and to perform catch
    for all AException inside B, then throwing BException.
    Second is to define error as int, and use #define.

    Thanks in advance,

    Alexander Lurye
  • Andrey Tarasevich

    #2
    Re: ENUM and inheritance

    Alexander Lurye wrote:[color=blue]
    > ...
    > class AException {
    >
    > public:
    > enum ERROR { INTERNAL, USAGE, GENERAL };
    > AException(ERRO R e):error(e) {}
    > inline ERROR GetError() const { return error; }
    > private:
    > ERROR error;
    > };
    >
    > A throws AException in case of some error.
    >
    > Now I need to write another class
    >
    > class B: public A;
    >
    > That have all types of errors A have, and also one more type (e.g.
    > PARSE).
    >
    > I have two solutions for the problem, but I don't like them.
    >
    > First is to define BException with it's own enum, and to perform catch
    > for all AException inside B, then throwing BException.
    > Second is to define error as int, and use #define.
    > ...[/color]

    Maybe you'll like the following approach more. It uses 'int' but still
    keeps the enums

    class AException {
    public:
    enum { INTERNAL, USAGE, GENERAL, NEXT_ };

    AException(int e) : error(e) {}
    virtual ~AException() {}

    int GetError() const { return error; }

    private:
    int error;
    };

    class BException : public AException {
    public:
    enum { PARSE = AException::NEX T_, SEMANTIC, IO, NEXT_ };

    BException(int e) : AException(e) {}
    };

    (Note the simple 'NEXT_' trick that makes maintaing and extending the
    exception codes easier.)

    That way you can throw 'BException' with excetion codes defined in
    'AException'

    throw AException(AExc eption::INTERNA L);
    throw BException(BExc eption::GENERAL );
    throw BException(BExc eption::IO);

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Rolf Magnus

      #3
      Re: ENUM and inheritance

      Alexander Lurye wrote:
      [color=blue]
      > Hi!
      >
      > I have the following problem:
      >
      > Suppose that we have the following class:
      >
      > class A;
      >
      > class AException {
      >
      > public:
      > enum ERROR { INTERNAL, USAGE, GENERAL };
      > AException(ERRO R e):error(e) {}
      > inline ERROR GetError() const { return error; }
      > private:
      > ERROR error;
      > };[/color]

      Why aren't you using an exception class hierarchy instead of error
      codes? Write classes InternalError, UsageError and GeneralError and
      derive them from class AException.
      [color=blue]
      > A throws AException in case of some error.
      >
      > Now I need to write another class
      >
      > class B: public A;
      >
      > That have all types of errors A have, and also one more type (e.g.
      > PARSE).[/color]

      That wouldn't be a problem with the class hierarchy approach (which is
      btw how exceptions are usually used). You'd just need to add a
      ParseError exception that also derives from the class AException.
      Another big advantage of using an excxeption hierarchy is that you can
      catch specific exceptions if you want, while with your approach, you'd
      always catch all AExceptions, no matter what type of error it actually
      represents.

      Comment

      Working...