Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Advanced
Jan 4, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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
 virtual promote(int rank);
 double salary;
 int rank;
 int department;
 bool temp;
employee emp;
ofstream ar("employees.dat");
 // write the entire emp object to a file
ar.write(reinterpret_cast < char * > (&emp), sizeof(emp));

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 file arc.read(dummy, 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 < char * > (&emp2); // copy data to the correct offset within emp2 arc.read(p+sizeof(void*), 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
arc.read(reinterpret_cast < char * > (&emp2), 
Danny Kalev
Thanks for your registration, follow us on our social networks to keep up-to-date