473,413 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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(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
Jul 22 '05 #1
2 2512
Alexander Lurye wrote:
...
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);

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2
Alexander Lurye wrote:
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.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hans De Schrijver | last post by:
Is it possible to extend the members of an Enum defined in an abstract class in a derived class? For instance: abstract class class1 { protected enum MyEnum {A, B, C} } public class class2...
16
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
10
by: Patrick B | last post by:
Is there a way to check if a certain variable is an enum? Example code: public enum MyEnum { Monday, Tuesday, Wednesday } public void MyMethod()
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
15
by: apm | last post by:
Can an enum start empty and be added to on the fly?
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
by: Brett Romero | last post by:
I'd like to create one enum that holds webpage name values and corresponding URL values. For example: enum webPageFileName {index = "index.aspx"} enum webPageName {index = "index"} enum...
2
by: RJ | last post by:
I was hoping to code a base class that would somehow require inheriting classes to define an Enum. Each inheriting child class needs to define it's own enum with it's own enumerated values, which...
1
by: chroot | last post by:
Hello Does anyone have a suggestion how to solve this in a better way? I have an abstract class A containing an std::vector<> of Mat (other class; not important here), with an enum specifying...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.