copying a buffer in a struct in C++ style -
say have struct
typedef struct { unsigned char flag, type; unsigned short id; uint32 size; } thdr; and buffer of data coming udp comunication, have buffer of bytes , size (data , data_size). data size bigger sizeof(thdr).
the struct @ beginning of buffer, , want copy struct defined in code, thdr struct_copy .
i know can use memcpy(&struct_copy,data[0],sizeof(struct_copy)); use "c++ style" way, using std::copy.
any clue?
how like
thdr hdr; std::copy(&hdr, &hdr + 1, reinterpret_cast<thdr*>(data)); should copy 1 thdr structure data hdr.
it can simpler:
thdr hdr; hdr = *reinterpret_cast<thdr*>(data); this last works in c well:
thdr hdr; hdr = *(thdr *) data; or why not create constructor takes data input? like
struct thdr { explicit thdr(const unsigned char* data) { thdr* other = reinterpret_cast<thdr*>(data); *this = *other; } // ... }; then can used such as
thdr hdr(data);
Comments
Post a Comment