0

I would like to save some settings made by the user to a file. I have a class called Project and would like to store the values of the member. I've worked a lot with Java and there I would do it with reflection, but now I have to find a solution for C++ (not having this feature).
My idea:

class Project
{
  Project();

  public:
  //Getter and Setter for private member...

  private:
    string   mProjectName;    //(="Test")
    string   mProjectCreator;
}

class ProjectParser
{
  ProjectParser();
  public:
    void SaveProject(Project* aProjectPtr)
    {
      //do stuff with the example below
    }
}

The ProjectParser would contain a pair list like this:
std::pair<[memberName],[memberFunction]>("projectName",&Project::GetProjectName)...


To save the member of a Project to a file the function SaveProject(Project* aProjectPtr) of the ProjectParser should be called and would do:

  1. Iterate the pairs with [memberName] and [memberFunction]
  2. Get return of [memberFunction] (together with the Project instance)
  3. Write value for [memberName] (for example in a JSON file or XML) projectName:"Test"

But beeing new to C++, I have no clue how to execute the member function for a instance of an object and how to get the return value. Is there a possibility to do it like this?

Drayke
  • 367
  • 1
  • 3
  • 14

1 Answers1

3

One thing you need to learn is that C++ doesn't have reflection. If you have the "name" of an object as a string, you can't easily get the object unless you already have all objects in a container.

So that means if you have a container of Project objects, say

std::vector<Project> projects;

Then you can easily find the project which have the mProjectName member equal to e.g. "projectName". You do this by using std::find_if:

auto iterator = std::find_if(begin(projects), end(projects), [](auto const& project)
{
    return project.mProjectName == "projectName";
});

Once you have the iterator, you can get the object and from that call any member function you like (directly or through pointers to member functions).

You can of course use functions and classes from the <functional> header file, like std::function and std::bind to bypass the need for direct pointers to member functions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • That's not quite that what I am looking for. I try to save the Project object into a file (with all members) and therefor I thought to assign `field names` for the members. So: mProjectName => "projectName" (field name in file) => Project::GetProjectName (for the value in file) – Drayke Mar 08 '17 at 10:10
  • 1
    @Drayke I think it is not very clear what do you mean by save the Project objects and how you think information should be retrieved.There is no out of the box way of doing it cause as you pointed out there is no reflection mechanism built in C++. One path you could start with is to register all the information you would like to store using macros (see [this](http://stackoverflow.com/questions/582331/is-there-a-way-to-instantiate-objects-from-a-string-holding-their-class-name)) but I think you want to register not only types but also members of this types and this could be kind of uncomfortable – W.F. Mar 08 '17 at 10:20