Home > Mobile >  Is it possible to override member access operators to detect when any member variable is modified?
Is it possible to override member access operators to detect when any member variable is modified?

Time:01-11

Say I have:

struct foo{

int bar;
int baz;
...
bool flag;
}

Can an access operator -> or . be overridden to detect if bar or any other member variable is modified ?

EDIT:

The purpose is if I have many member variables and any of them is changed, I have a quick way of setting a flag, instead of using setters to encapsulate all the variables, making the code verbose.

CodePudding user response:

Your approach is flawed because even if you override access operators you will not catch pointers writing the actual memory.

If most of the variables have the same type you can use an enum for flags and a single function to set or get a specific variable.

For example:

private:
  int bar;
  int baz;

public:
  enum IntVariables { varBar, varBaz };
  bool flag;

  void setVariable(int varId, int value) {
    flag = true;
    if (varId == varBar)
      bar = value;
    else if (varId == varBaz)
      baz = value;
  }

CodePudding user response:

I considered the following approach:

Just use a wrapper class that can have any data type, but implement all operations. In this same wrapper class override operators, and use the wrapper class in other class that require any modifications of member variables to be detected.

template <class T>
class wrapper {
  private:
    T var;
    ... .. ...
  public:
    T doSomethingToVar(T arg);
    ... .. ...
    //Wherever the variable is modified send out a notification to whomever needs to detect the changes.
};

Pros:

  • When declaring variables in whichever class needs to detect modification of variables, it is easy to declare using the wrapper, without much additional code.
  • To ensure modifications are detected, need to implement functions / getters / setters / overload operators to detect modifications. This is tricky, and requires some thought.

Cons:

  • Tricky to implement a general purpose wrapper that can detect all modifications, since complex types can have functions that modify themselves in ways one is not aware of.

Notes: How to ensure that every method of a class calls some other method first?

This answer is a work in progress, and I think it may be useful to others and maybe just cool to know about eventually, so open to comments. Will keep updating.

Update:

While writing out the above answer, I considered a different approach, of shifting responsibility onto the member variable classes:

class DetectChanges{

void onDetectChanges(){ //This function should be called by all implementing classes when the class has changes. }

Can make it a design choice that all member variables inherit from DetectChanges.

The above two approaches are what I'm considering now. Not a solution yet, but thought I would put it out for comments and see if eventually we can figure something out. }

  • Related