Background / Setup
For a project I am working on, I have a class which describes a message that is sent/recieved by a client/server. Getting the proper members initialized was a big hurdle for me, but this stackoverflow question gave me a lot of insight on how to implement the basic functionality.
below is a somewhat simplified version of my code right now (I ommitted extraneous information which doesn't pertain to the question):
class Message
{
public:
enum class tags_t {T1, T2, T3};
// initializes a message and union member associated with T1
Message(tags_t tag);
Message() : Message(tags_t::T1) {};
class T1
{ // non-trivial member data };
class T2
{ // non-trivial member data };
class T3
{ // non-trivial member data };
void set_data(const T1& d);
// all members ...
void get_data(T1& d);
// all members ...
// destructs old type, constructs new type
Message& operator=(const Message& other);
private:
tags_t m_tag;
union
{
T1 u_t1;
T2 u_t2;
T3 u_t3;
};
};
My Goal
I want to add a member to access (and limit access) the union member data. (ie, only access a union member if it has been initialized). When I use my code, I want the usage to look something like this:
Set
Message msg(Message::tags_t::T1); // initialize message
message.data = { /* T1 */ }; // assign message data
Get
Message::T1 t1;
Message msg();
socket.recieve(msg); // this could be T1, T2, or T3, but T1 is expected
t1 = msg.data; // or: t1 = msg.data(); // since msg knows what type it is, it should be able to
// assign t1 if m_tag == tags_t::T1
essentially, I want some fancy c++ way to get/set the message data. I thought about adding a member called data in Message, which is a subclass with its assignment operators overloaded, like this:
class Message
{
...
class D
{
// some operator overloads
// and c++ magic
} data;
...
};
but I was unable to arrive at a solution.
TLDR: I feel like there should be a way to create a accessor member of Message which will automatically get/set the union members based on the type of the other operand of the assignment operator, provided that their types match.
I hope I was able to articulate my question sufficiently, just let me know if you need me to explain anything in more detail. Thanks in advance for taking time to help me out!