Saving & loading paired objects to/from data files

97 views Asked by At

I have two classes, A and B, whose member object data will be saved in two text files, A_file and B_file. Class A includes class B. (Specifically, class A contains a pointer to a class B object.) I am wondering how to save & load data to & from the files.

I assume that objects of one class will need identification numbers and that objects of the other class will have to store those numbers so that the correct objects are paired. (Which class should hold those numbers? And should I use a dedicated member variable, e.g. int idnumber, or, since the number won't be used during runtime, would each object's current memory address suffice?)

As far as actually instantiating objects, I think that I have three choices:

  • I could open A_file first and instantiate class A objects. Then, when I open B_file and instantiate each class B object, I could assign it to the corresponding class A object's pointer.
  • I could open B_file first and instantiate class B objects. Then, when I open A_file and instantiate each class A object, I could assign the corresponding class B object to its pointer.
  • I could open both files and instantiate all A and B objects and then, after that is done, pair up the corresponding class A objects and class B objects. (I assume that this would be slower, but perhaps it would be simpler?)

btw, I can't post actual code, since I'm thinking about how to structure such a program before coding anything. I want to know whether there are any common best practices, or whether anything is acceptable and it really just depends. TIA.

0

There are 0 answers