mercredi 1 août 2018

How do I notify listener which field of a struct is updated?

Actually, my question is related to language (I'm using C++).

I have a class

sturct StudentInfo
{
  // lots of fileds
  string id;
  string name;
  int age;
  double height;
  ......
};

class IListner
{
    void OnStudentInfoChanged(StudentInfo aLatestInfo);
};
void ListenStudnetInfoChange(IListener* aListener);

I want that if a user received the callback OnStudentInfoChanged

The receiver can distinguish which fields are being updated.

I don't know if there is an elegant pattern dealing with this problem.

My intuitive solution is to provide a 1-1 mapping enum

like

enum StudentInfoFields 
{
  field1,
  field2,
  .... 
};

and change

void OnStudentInfoChanged(StudentInfo aLatestInfo);

to

void OnStudentInfoChanged(vector<StudentInfoFields> aWhichAreChanged,
                          StudentInfo aLatestInfo);

It's not a good idea I think, but I don't have any idea to let a user know.

Please give me some suggestions :)

Thanks.

Aucun commentaire:

Enregistrer un commentaire