473,394 Members | 1,717 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,394 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 7391
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.