serialization - Deserialization of Objects in C++ in the Constructor -


it practice deserialize object using overloaded constructor in c++ or better create member function deserialize(std::istream& file)?

yes is. avoid two-step initialization , problems caused it. opjects don't make sense exist "uninitialized". @ least in such cases, should put serialization code constructor. advantage of design is, can use same constructor reading , writing. , you're unable violate order of initializing of base classes or member classes. disadvantage writing temporarily create copy of object written somewhere. if turns out problem, can still copy constructor code write method. of course should part of current century , use exception handling errors when reading , writing. , want make creadwrite class constructable abstract cread , cwrite, can read , write from/to "somewhere", including files, pipes, sockets, memory buffer, , knows customers going come day after tomorrow.

struct creadwrite;  struct a:b {   std::string m_sname;      a(const *const pwrite, creadwrite *const pfile)         :b(pwrite ? static_cast<b*>(pwrite) : 0, pfile),         m_sname(pfile->readwrite(pwrite ? &pwrite->m_sname : 0))     {     } }; 

reading:

creadwrite sfile("test.dat", "r"); sa(0, &sfile); 

writing:

creadwrite sfile("test.dat", "w"); sa; a(&sa, &sfile); 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -