Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

is it possible to simply serialize C++ objects

Can you cast an object to a string of hex data (similar to how packets are sent) and then store that and then cast the object back? I know its possible with C structs which are basically objects undearneath in C++.

Compatibility of the serialization across different systems isn't important.

auto obj = new Something();

auto objHex = (unsigned char*) obj;

// store objHex in like a db
// retrieve objHex

auto obj2 = new Something();  // allocate
*obj2 = (Something*) objHex;  // set the dereference
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No. Reasons:
a) Dynamic allocated memory, ie. pointer in the struct/class.
b) Endianess, int-size, padding etc. of the struct, order of the members...

Other things:
"If" it would be possible, there is no reason to create a full object
with constructor call before overwriting it.
And in my opinion, you′re overusing auto.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.5k users

...