CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Location
    Far away
    Posts
    2

    Question Object Serialization

    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
    Help me help you

  2. #2
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    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:

    Code:
    void CPerson::load( std::istream & Input )
    {
    // use cin in here
    }
    
    void CPerson::save( std:: ostream & Output )
    {
    // use cout in here
    }
    Hope that helps
    Martin Breton
    3D vision software developer and system integrator.

  3. #3
    Join Date
    May 2002
    Location
    Far away
    Posts
    2

    Unhappy

    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
    Help me help you

  4. #4
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    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.

    Code:
    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
    }
    Martin Breton
    3D vision software developer and system integrator.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured