By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Alternative to global enum header file?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"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

P: n/a
> 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 discussion thread is closed

Replies have been disabled for this discussion.