0

I have a class which I'll name CObject with a lot of private data members..

class CObject
{
   private:
   int a, b, c, d, e, f, g, h, i;
   string j;
   ...More data types etc
};

And another class which parses an initialisation file recursively finding members of objects.. This class is satisfying a virtual class provided by a library.

class CParser
{
   public:
    void beginParsingObject()
    {
      //etc
    }

    void parseObjectMembers(string name, int a)
    {
       //Found a member variable of the object!
    }

    void finishParsingObject()
    {
       //Finish up parsing the object
    }
};

So "parseObjectMembers" would be called as many times as there are member variables in the CObject class.

The CObject class will have it's member variables populated when the parser finds it's next member variable in the entry of it's data files.

Here is my quandary:

What would be a good way to set the member variables in CObject as the data is read?

-Using a constructor would mean caching every value that comes through, and constructing the object on completion of parsing the object - less than satisfactory as there's a lot, and I want the parser to be pretty generic.

-Using getters and setters in the CObject class would expose all the member variables to everything else, where that's not needed or wanted.

-Using an initialise function would be fine, but I don't want anything else having access to that function - a friend function maybe?

Any ideas are appreciated, and I'll clarify where I can. Sorry the code is obscure and not very fleshed out, but hopefully it's enough to see the crux of the problem.

Thanks

SomeGuy
  • 3
  • 2

1 Answers1

0

What you describe seems to be serialization / deserialization. It is very common to backup / retrieve objects data from (XML) configuration file. Maybe a good start is Boost Serialization Tutorial.

More basically the answer to your question could be "use a 'friend' function / class of the class itself".

NGI
  • 852
  • 1
  • 12
  • 31
  • Yes thank you for putting a name to a concept. This is indeed serialisation. That should help point me in the right direction. – SomeGuy Oct 26 '16 at 03:50
  • Yes giving the right name is important. Maybe also add "serialization" to the title of your post. Another example is C# that I mention even if this post is C++ . I basically used the mechanism of XMLSerializer which requires public "Properties" (fields accessed through method calls) with getter and setter and a parameterless constructor. Have a look just for information at [question on Serializable](http://stackoverflow.com/questions/2982376/why-is-serializable-attribute-required-for-an-object-to-be-serialized). – NGI Oct 26 '16 at 04:01
  • I mentioned XML files but there are also binary formatter / backup files. However I assume that ( for what I know ) most of the time the data for one instance will be contiguous and not spread all over the file. It would need for each data member to re-specify to which instance this member belongs – NGI Oct 26 '16 at 04:05