Serializing a Polymorphic Object

Serializing a Polymorphic Object

A polymorphic object has one or more virtual functions. When you serialize such an object to a file so that it can be reconstituted later, the deserialization might rewrite its vptr and cause undefined behavior. To overcome this problem, leave the vptr intact by copying only the user-declared data members from the file. The vptr’s offset within a class may vary from one compiler to another. Therefore, you need to calculate it first. The following tip explains how this can be done. Consider the following class:

 class employee{public: virtual promote(int rank);//..private: double salary; int rank; int department; bool temp;};employee emp;ofstream ar("employees.dat"); // write the entire emp object to a filear.write(reinterpret_cast  (&emp), sizeof(emp));ar.close();

The write() member function copy all the data members of emp to a file, including the vptr. Assuming that our compiler places the vptr at offset 0, we can reconstitute the archived object as follows. First, we create an empty employee object:

 employee emp2;

Then we copy the first archived data member from the file to a dummy variable: employee emp2;ifstream arc(“employees.dat”);char dummy[sizeof(void *)]; // a dummy read; advance past the vptr in the, sizeof(void *));
The first read() call simply advanced the file’s pointer past the vptr. Now we can copy the archived data members to the object: char *p= reinterpret_cast (&emp2);// copy data to the correct offset within*), sizeof(employee)-sizeof(void*));
If your compiler places the vptr after all user-declared data members, it’s even easier:

 employee emp2;ifstream arc("employees.dat");// copy data to the correct offset within  (&emp2),  sizeof(employee)-sizeof(void*));


Share the Post: