ENUM and inheritance

  • Thread starter Alexander Lurye
  • Start date
A

Alexander Lurye

Hi!

I have the following problem:

Suppose that we have the following class:

class A;

class AException {

public:
enum ERROR { INTERNAL, USAGE, GENERAL };
AException(ERROR 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
 
A

Andrey Tarasevich

Alexander said:
...
class AException {

public:
enum ERROR { INTERNAL, USAGE, GENERAL };
AException(ERROR 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.
...

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::NEXT_, 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(AException::INTERNAL);
throw BException(BException::GENERAL);
throw BException(BException::IO);
 
R

Rolf Magnus

Alexander said:
Hi!

I have the following problem:

Suppose that we have the following class:

class A;

class AException {

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

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.
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).

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,898
Members
47,436
Latest member
MaxD

Latest Threads

Top