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 < 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 filearc.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 emp2arc.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 emp2arc.read(reinterpret_cast < char * > (&emp2),  sizeof(employee)-sizeof(void*));
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a