Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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 file
ar.write(reinterpret_cast < char * > (&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 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), 
 sizeof(employee)-sizeof(void*));
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap