Vivek Kumar
Vivek Kumar

Reputation: 5040

Observing change in value of member variable

I want to get notification on modification of some class member-variable(may be primitive or user-defined).

Can anyone let me know how to achieve this.

Thanks

Little more clarification to my question:

I have scenario something like

I have existing menu and menu-items. Now I am putting all these menu-items on toolbar. Since when these menu-items was inside menu-item I get event ON_UPDATE_COMMAND_UI where I call a function and update menu-items states.

But now for toolbar I have to put function for checking state every time, variables(which are many in number and also gets modified at many state, for which I have no control).

Can anyone suggest me some good alternative, without changing the existing code, since it's very fragile.

Is there any way we get some variable change notification from language/compiler, such as the way the Watch variable works in visual studio debugger.

Upvotes: 2

Views: 2796

Answers (3)

Alok Save
Alok Save

Reputation: 206566

Since you mention notifications(which indicates receiving intimations asynchronously), What you need is an:
Observer design pattern

Upvotes: 4

Nicola Coretti
Nicola Coretti

Reputation: 2685

I would recommend you implement the observer pattern.

Upvotes: 3

iammilind
iammilind

Reputation: 70030

You can make the variable private and access it through getter/setter methods which are publicly available. Put the print or your custom asserts inside those getter/setters.
e.g.:

class A {
private:
  int x;
public:
  int getx () const { /* your code */ ; return x; }
  void setx (const int i) { /* your code */; x = i; }
};

Upvotes: 3

Related Questions