Click to See Complete Forum and Search --> : Object Serialization


Sigma
May 28th, 2002, 10:46 AM
Hi

I have 2 classes Person and Car

class CPerson
{
public:
CPerson();
virtual ~CPerson();
void getData(void) {
cout << "Name : " ; cin >> name;
cout << "Age : " ; cin >> age;
}

void showData(void)
{
cout << "\n Name : " << name;
cout << "\n Age : " << age;
}

public:
int age;
char name[14];
CCar itsCar;

};

class CCar
{
public:
CCar();
virtual ~CCar();

public:
int year;
char model[10];

};

Here is my main

#include "stdafx.h"
#include "Person.h"

int main(int argc, char* argv[])
{
CPerson pers;

pers.getData();

pers.itsCar;

cout << "Car Year : " ; cin >> pers.itsCar.year;
cout << "Car Model : " ; cin >> *pers.itsCar.model;

ofstream outfile("person.dat", ios::binary);
outfile.write( (char*) &pers, sizeof(pers));


CPerson me;

ifstream infile("person.dat", ios::binary);

infile.read( (char*) &me, sizeof(me));
me.showData();
cout << "\nCar Model : " << *me.itsCar.model;
cout << "\nCar Year : " << me.itsCar.year;

return 0;
}


I want to serialize Person and itsCar, I can save properly but how can I restore itsCar.

itsCar was a pointer before I changed it to object.

I can not restore the related car.

I have tried to serialize CPerson only and it worked.

AnyOne can help please!

Your help will be rated.

PS : I am not using MFC:rolleyes:

proxima centaur
May 28th, 2002, 10:52 AM
you can restore by doing exactly the opposite of your serializer method.

Might I suggest you always use coherent method names for well known functionalities? please use methods named serialize() and unserialize() or load() and save(). It's so much clearer for others.

use cin to read from the datafile.

I suggest you use the following method signature:


void CPerson::load( std::istream & Input )
{
// use cin in here
}

void CPerson::save( std:: ostream & Output )
{
// use cout in here
}

Hope that helps

Sigma
May 28th, 2002, 11:00 AM
In my code, I tried to restore it the oposit way just like you said.
If you look at the read line

infile.read( (char*) &me, sizeof(me));
unfortunatly it didn't work :(

proxima centaur
May 28th, 2002, 11:28 AM
I didn't see that in your code.

All I can say is that you play dangerously!!!

I think maybe the fact that there are virtual destructors can be a cause of mishap because there is a virtual table to be taken into account.

It is generally not a good idea to proceed in that way.

I suggest you implement a load( ) and save() method in your class CPerson, and in your class CCar and you call the CCar::load() method from the CPerson::load() method.

in your save method save all your data members.
in your load method, load all your data members.

This is the most portable and safe way to serialize objects.


void CPerson::load( std::istream & Input )
{
// load CPerson datamembers
itsCar.load( Input );
}

void CPerson::save( std:: ostream & Output )
{
// save CPerson datamembers
itsCar.save( Output);
}

void CCar::load( std::istream & Input )
{
// loadd CCar datamembers
itsCar.load( Input );
}

void CCar::save( std:: ostream & Output )
{
// save CCar datamembers
}