Object Persistence

This is a discussion on Object Persistence within the C++ Programming forums, part of the General Programming Boards category; Can anyone point me in the right direction please I need to stream an object to file using one program ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    Object Persistence

    Can anyone point me in the right direction please

    I need to stream an object to file using one program so that a different program can use this objects data and methods. The second program will know about the interface methods the object provides and that all. The functionality of the object must be passed in.

    I hope this makes sence

    Thanks for any help

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    66
    The functionality of the object must be passed in.
    This is very non-portable in C++ as the code is CPU/platform specific. You want to write a distributed system that allows objects to transfer themselves anywhere?! Or are you just looking for a way to store data?

    If it is only data persistence, try reading about XML exporting and importing, which is the most generic way to tranfer data between objects/platforms/langauges/OSs.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'd have the object be able to write/read itself to/from file. Then progam A can create an object, write it to file where it will be stored. Program B can then read the object data from file to be used in that program.

    Code:
    //MyClass.h
    class MyClass
    {
       public:
           MyClass();
          void setData(int);
          int getData();
          void writeToFile();
          void readFromFile();
       private:
          int data;
    };
    
    //MyClass.cpp
    #include <fstream>
    MyClass::MyClass()
    {
        data = 0;
    }
    
    void MyClass::setData(int input)
    {
       data = input;
    }
       
    int MyClass::getData()
    {
       return data;
    }
    
    void MyClass::writeToFile()
    {
       ofstream fout("myData.txt");
       fout << data;
    }
    
    void MyClass::readFromFile()
    {
       ifstream fin("myData.txt")
       fin >> data;
    }
    
    
    //prgram A
    #include <fstream>
    #include "MyClass.h"
    
    int main()
    {
       //create object
       MyClass  myObject;
       cout << myObject.getData();
       int newData = 3;
       myObject.setData(newData);
       cout << myObject.getData()
       myOjbect.writeToFile();
    }
    
    //program B
    #include <fstream>
    #include "MyClass.h"
    
    int main()
    {
       //create object
       MyClass myObject2;
       cout << myObject2.getData();
       myObject2.readFromFile();
       cout << myObject2.getData();
    }
    endless variations as desired.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    XML for object data, and a scriptng language for the functionality. There isn't any simple way to "save" the functionality other than the translate it into something that can later be read and "understood".
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    Program B will be written before the internal working of 'the object' is know. Only the interface methods are know at this time.

    When program A is written not only is 'the object class' fully defined but the data content of the required instance is known.

    The OS is MS Windows.

    I could save the class as a DLL and stream the object data to a separate file containing the DLL name. This is similar to ZackL suggestion, but alas not ideal.

    Any other suggestions would be greatly appreciated

    Thanks once again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21