c++ - How to read and write shared_ptr? -


when data use shared_ptr shared number of entries, way read , write data show sharing? example

i have data structure

struct data {     int a;     int b; };  data data; data.a = 2; data.b = 2; 

i can write out in file data.txt as

2 2 

and read file, can data values a = 2 , b = 2. if data uses share_ptr, becomes difficult. example,

struct data {     shared_ptr<int> a;     shared_ptr<int> b; };  data data; 

data can

data.a.reset(new int(2)); data.b = data.a; 

or

data.a.reset(new int(2)); data.b.reset(new int(2)); 

the 2 cases different. how write data data.txt file , read file data, can same data same relations of a , b?

this kind of data serialization problem. here, want serialize data has pointer types within it. when serialize pointer values, data pointing written somewhere, , pointers converted offsets file data.

in case, can think of int values being written out right after object, , "pointer" values represented number of bytes after object. so, each data in file like:

|total-bytes-of-data| |offset-a| |offset-b| |value[]| 

if a , b point same instance, have same offset. if a , b point different instances, have different offsets.

i'll leave exercise problem of detecting , dealing sharing happens between different data instances.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -