Alternative to global enum header file?

N

Nobody

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
 
P

Peter van Merkerk

Nobody said:
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.
 
N

Nobody

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top