473,325 Members | 2,608 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,325 software developers and data experts.

Alternative to global enum header file?

Hi All,

In my application I have the following implementation for ClassA and ClassC
invoking a call on ClassB.

void ClassA::DoSomething()
{
int value = m_pClassB->Read(enum1);
}

void ClassA::DoSomethingElse()
{
int value = m_pClassB->Read(enum2);
}

------------------------------------------------

void ClassC::DoSomethingElseAgain()
{
int value = m_pClassB->Read(enum3);
}

------------------------------------------------

int ClassB::Read(enum tag enumValue)
{
switch (enumValue)
{
case enum1:
// invoke a call on ClassX, do something and return an int

case enum2:
// invoke a call on ClassY, do something different and return
an int

case enum3:
// invoke a call on ClassZ, do something different again and
return an int
)
}

------------------------------------------------

Some other details:
The enums are defined in a header file that is accessible to ClassA, ClassB
and ClassC.
ClassB is derived from an abstract base class.
ClassB uses the id parameter to determine which operation to call on which
class. I think this might be a use of the Facade pattern.
I come form a C background and am relatively new to OO and C++.

Although this works fine, this mechanism exists throughout the application
for other interfaces and I feel that there is some elegant way of doing this
that I have missed.
Is there a better way to implement this or am I OK with what I've got?
Specifically an alternative to the use of the enum header file and the
switch statement.
I would like to keep the abstract base class and the idea of a message id to
give to ClassB so that it knows which class the Read() is for when invoked
by ClassA or ClassC.
Thanks


Jul 22 '05 #1
2 7387
"Nobody" <No****@Nobody.co.uk> wrote in message
news:5H***********@news-binary.blueyonder.co.uk...
Hi All,

In my application I have the following implementation for ClassA and ClassC invoking a call on ClassB.

void ClassA::DoSomething()
{
int value = m_pClassB->Read(enum1);
}

void ClassA::DoSomethingElse()
{
int value = m_pClassB->Read(enum2);
}

------------------------------------------------

void ClassC::DoSomethingElseAgain()
{
int value = m_pClassB->Read(enum3);
}

------------------------------------------------

int ClassB::Read(enum tag enumValue)
{
switch (enumValue)
{
case enum1:
// invoke a call on ClassX, do something and return an int

case enum2:
// invoke a call on ClassY, do something different and return an int

case enum3:
// invoke a call on ClassZ, do something different again and return an int
)
}

------------------------------------------------

Some other details:
The enums are defined in a header file that is accessible to ClassA, ClassB and ClassC.
ClassB is derived from an abstract base class.
ClassB uses the id parameter to determine which operation to call on which class. I think this might be a use of the Facade pattern.
I come form a C background and am relatively new to OO and C++.

Although this works fine, this mechanism exists throughout the application for other interfaces and I feel that there is some elegant way of doing this that I have missed.
Is there a better way to implement this or am I OK with what I've got?
Specifically an alternative to the use of the enum header file and the
switch statement.
I would like to keep the abstract base class and the idea of a message id to give to ClassB so that it knows which class the Read() is for when invoked by ClassA or ClassC.


Your problem description is not really concrete enough to make a good
recommendation. It didn't describe in what way the invokations in made
ClassB::Read() are different. However the code you posted look a bit
messy though. Maybe polymophism is what you are looking for:

class Base
{
public:
virtual int do_something() = 0;
};

class ClassX: public Base
{
public:
virtual int do_something() {return 123;}
};

class ClassY: public Base
{
public:
virtual int do_something() {return 456;}
};

class ClassZ: public Base
{
public:
virtual int do_something() {return 789;}
};

int ClassB::Read(Base* obj)
{
return obj->do_something() * 2;
}

The advantage of this is you can add new classes derived from Base,
without changing globally defined enums or changing the ClassB::Read()
member function.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Jul 22 '05 #2
> Your problem description is not really concrete enough to make a good
recommendation. It didn't describe in what way the invokations in made
ClassB::Read() are different. However the code you posted look a bit
messy though. Maybe polymophism is what you are looking for:


I'll add more detail to clarify the problem. The read function in ClassB is
inherited from the abstract base class.

#include "enums.h"
class Base
{
public:
virtual int Read(enum enumValue) = 0;
};

------------------------------------------------------------

#include "enums.h"
#include "Base.h"
#include "X.h"
#include "Y.h"

class ClassB: public Base
{
public:
virtual int Read(enum enumValue)
{
switch (enumValue)
{
case enum1:
return objX->do_something();

case enum2:
return objY->do_something_different();

case enum3:
return objY->do_something_else();
}
}
};
------------------------------------------------------------

#include "enums.h"
#include "Base.h"

class ClassA
{
public:
Base * m_pBase;

void any_old_function()
{
// do some stuff
int retval = m_pBase->Read(enum1);
}

private:
void another_function()
{
// do some stuff
int retval = m_pBase->Read(enum2);
}
};
#include "enums.h"

------------------------------------------------------------

#include "Base.h"

class ClassC
{
public:
Base * m_pBase;
private:
void yet_another_function()
{
// do some stuff
int retval = m_pBase->Read(enum3);
}
};
The enums are defined in enums.h and are globally accessable.
ClassB uses the enum parameter to determine which operation to call on
ClassX or ClassY.

I'm surprised at the lack of response. Is the question too trivial for this
newsgroup or something? I would have thought many of you out there have come
across this before. I have searched and read extensively for a
more elegant solution without any success. Please help.

Thanks.


Jul 22 '05 #3

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

Similar topics

0
by: Nobody | last post by:
Hi All, In my application I have the following implementation for ClassA and ClassC invoking a call on ClassB. void ClassA::DoSomething() { int value = m_pClassB->Read(enum1); }
2
by: Sandy | last post by:
I have an enum like this file1.cpp enum e {e1, e2}; extern void fun(); void main() { fun(); }
0
by: Alf P. Steinbach | last post by:
Is there any way to get the "enabling" traits definitions textually closer to (ideally on the line below) the enums they refer to, in the code below? template< bool shouldBeTrue > struct...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
7
by: Method Man | last post by:
Can someone explain the scope/linkage differences between the following 4 global declarations and when one should be used (theoretically) over the rest? sample.h --------- #ifndef SAMPLE_H...
12
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
13
by: Rohit | last post by:
Is there any way by which I can find number of elements in an enum list? Generally I use one last element at the end to find out total number of elements. e.g. typedef enum{ SUN, MON, TUE,...
8
by: benn | last post by:
Here's the setup... Defines.h file contains: enum DAY { monday, tueday }; DayFunctions.h contains prototype: void printIsMonday ( enum DAY currentDay); DayFunctions.c contains:
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.