physical data with different logical views, back and forth

Dear forum,

I have the following problem. In a simulator I have a physical data type, a mail. Depending on receiver and contents the data type should be interpreted in different was. If I was using C I would use a union.

// Physical representation
class Mail

{

public:


Mail(void);

Mail(int payload);

public:

~Mail(void);

private :

int m_payload

int m_addressee;

int m_sender;

};



m_payload can be different information depending of the type of mail. For example, there could be a ConfigMail where the MSB of m_payload is a reset information. ConfigMail should provide a service for getting/setting the reset information. The data could also be public, I do not know what the best approach is. Now, the same bit could be something completely different in another mail type.

I need a constructor that takes the bas Mail and creates the arbitrary subtype ConfigMail for example. I also need a way to create the base Mail from any arbitrary mail type. One way to put this is that the Mail class is the physical representation and I need services for a number of logical views of the class and the possibility to convert the logical views back to the Mail class.

How is this done in the best way?


Br Mikael Carlsson
Last edited on
You may want to use inheritance. I've left all your data fields as int since I don't know what they should be.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Mail {
public:
    Mail( int to, int from ) : to_(to), from_(from) { }
    ~Mail(); // you may want this to be virtual
private:
    int to_;
    int from_;
};

class ConfigMail : public Mail {
public:
    ConfigMail( int to, int from, int payload )
      : Mail( to, from ), payload_( payload ) { }
    ~ConfigMail();
    int  getResetInfo()              { return payload_; }
    void setResetInfo( int payload ) { payload_ = payload; }
private:
    int payload_;
};

Topic archived. No new replies allowed.