473,587 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::DoSomet hing()
{
int value = m_pClassB->Read(enum1);
}

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

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

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

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

int ClassB::Read(en um 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 7412
"Nobody" <No****@Nobody. co.uk> wrote in message
news:5H******** ***@news-binary.blueyond er.co.uk...
Hi All,

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

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

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

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

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

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

int ClassB::Read(en um 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(Ba se* 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.merke rk(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_d ifferent();

case enum3:
return objY->do_something_e lse();
}
}
};
------------------------------------------------------------

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

class ClassA
{
public:
Base * m_pBase;

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

private:
void another_functio n()
{
// 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_fun ction()
{
// 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
742
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
5485
by: Sandy | last post by:
I have an enum like this file1.cpp enum e {e1, e2}; extern void fun(); void main() { fun(); }
0
1178
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 StaticAssert_; template<> struct StaticAssert_<true> {}; template< typename T > struct EnumIncrementEnabled_{ enum{ yes = false }; };
11
2539
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 opaque data types to transfer information in between them. At some stage I was stuck and needed to make a variable global, and I also needed to make the struct declaration public to some other parts. Looking through the code I found out that lots...
1
2746
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 colors are referred in different subsystems in our projects. They are defined as enumeration constants and a single color must be the same value all across our projects.
7
2587
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 #define SAMPLE_H int a;
12
6692
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: public enum Days { Sunday };
1
29333
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
13
4793
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, EndOfList }weekdays;
8
8524
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
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
5719
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.