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 !
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 !