I am trying to implement a message that contains sub messages. The format would look something like the following.Message: Field 1, field 2, ..., sub type, bufferDepending on the sub type there could be one of several ways to interpret the buffer. Some of the sub messages might look something like the following.Sub type 1: Field 1, field 2, ..., sub sub typeSub sub type 1: Field 1, field 2, ..., data bufferWhat I'm not sure about is how to structure my code in order to best represent the structure that exists. The sub messages don't inherit from each other, so I don't want to use inheritance.Each of the fields represented in the message could be anywhere from 1 to 31 bits in length. Ideally I would like to have a struct/class with all unsigned int data types to make it easy to interact with the data and let the serialize/deserialize methods handle packing and unpacking the data before/after sending/receiving the buffer on the network.
How to represent message with sub messages?
c++;data structures;networks
You may structure your object classes using composition.  This means referring in your message object to another object that will hold the sub type.struct Message {     unsigned message_type;     unsigned field1;     ...    unsigned fieldn;     Message *sub_message;  // if there's a dedicated type for this};If any sub type could have sub sub types, which may have sub sub sub types, and so on,  then you should consider using a composite design pattern.   If your payload contains mostly unsigned integers, then you could make your classes easier to use with an array or a vector of unsigned integer.  This can help to avoid redundant code: instead of copy-pasting a lot of code to replicate similar behavior but for each field (for example print it on the screen, or serialize it), you could use a loop instead.  struct Message {     unsigned message_type;     vector<unsigned> field; // with a vector, the number of field could be variable      Message *sub_message;  // if there's a dedicated type for this};You could then define somewhere some rules, like for example the number of fields to use for each specific subtype.  