|
|
|
|
|
by radarsat1
3549 days ago
|
|
The first example could have been implemented in C or C++ using a union. Wouldn't necessarily be type-safer, but the reminder of writing myConnection.connected.m_id instead of just myConnection.m_id is also a pretty good way to ensuring that you remember to check if (myConnection.m_connectionState == CONNECTED) before ever accessing myConnection.connected. That said, having compiler errors is much better. I'd love to be able to achieve this in pure C. One way might be to use opaque types with accessor functions to return a pointer to the correct part of the union according to the requested connection state, a bit like std::variant::get_if. |
|