c++ - Initialize union with array? -
i new c++. want create data structure network communication (tcp), can build byte array small tagged pieces (like in serialized class). , of course need reverse behavior of this. need tagged pieces byte array, that's why put constructor union.
typedef union message { struct { int header; int payload; } pieces; int whole[2]; message (int* arr) { (int = 0; < 2; i++) { whole[i] = arr[i]; } } message ():ival(){} } message ; main() { int a[2] = {10, 2}; message msg(a); }
this snippet working. i'm curious: there better solution initialize union array?
Comments
Post a Comment