Using different types depending on run-time input

N

namo

Hello,

I have started a small simulation project to learn C++ and I am faced with
the following problem : the user can choose at runtime to simulate protocol
P1 or P2.
P1 uses packets of type
enum P1_pkt_t { A, B };
and P2 :
enum P2_pkt_t { A, C };

I would like to abstract this packet type in a general_pkt_t so that I can
define, for instance,
class Packet { ... general_pkt_t type_; ... }
class Node { ... queue<Packet> queue_; ... }

where Node will probably be a virtual base class for Node_P1 and Node_P2
anyway.

One obvious solution, in C, would be to define an enum that is the reunion
of the ones above :
enum { A, B, C }
and then deal with the separate cases in the code, but I am wondering if
there is a more elegant solution in C++ ?
I came up with intricate solutions involving wrapping the enum-s in classes
and using templates and inheritance... but they didn't actually work ! :)

Any advice appreciated, thanks !
 
D

Daniel T.

namo said:
Hello,

I have started a small simulation project to learn C++ and I am faced with
the following problem : the user can choose at runtime to simulate protocol
P1 or P2.
P1 uses packets of type
enum P1_pkt_t { A, B };
and P2 :
enum P2_pkt_t { A, C };

I would like to abstract this packet type in a general_pkt_t so that I can
define, for instance,
class Packet { ... general_pkt_t type_; ... }
class Node { ... queue<Packet> queue_; ... }

where Node will probably be a virtual base class for Node_P1 and Node_P2
anyway.

One obvious solution, in C, would be to define an enum that is the reunion
of the ones above :
enum { A, B, C }
and then deal with the separate cases in the code, but I am wondering if
there is a more elegant solution in C++ ?
I came up with intricate solutions involving wrapping the enum-s in classes
and using templates and inheritance... but they didn't actually work ! :)

Any advice appreciated, thanks !

Sounds like a job for an Abstract Factory Pattern. The concrete factory
is determined at runtime by the user input, then the factory creates
packets for the rest of the program to use.

BTW, this type of question should probably be asked in a design forum
such as comp.object rather than a language forum...
 
N

namo

Sounds like a job for an Abstract Factory Pattern. The concrete factory
is determined at runtime by the user input, then the factory creates
packets for the rest of the program to use.

I will investigate that pattern.
BTW, this type of question should probably be asked in a design forum
such as comp.object rather than a language forum...

I posted here because I thought there would be some relevant language
features. Out of ignorance...

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top