"lou zion" <il********@adelphia.net> wrote in message
news:Jf******************************@adelphia.com ...
hey all,
i've got a class that A that has a static class member
static int MajorMode;
it's important that if one instance of the class changes this variable,
all instances react immediately. while they'll all see the new value of
the variable, how do you get them each to execute a function when
MajorMode changes?
thanks -- lou
That's where wrappers come in. MajorMode should be private, and anything
that changes it should have to go through interfaces, SetMajorMode or
GetMajorMode.
In your case SetMajorMode would need to, somehow, call a function on each
instance of your class. How you do that is the quandry. If all your
instances are in a vector, pass a reference to the vector to SetMajorMode.
Otherways would be to somehow keep references to each instance of your class
in a static vector, somehow, and iterate through that. Then you would only
create your class through a factory that also added a reference to the
vector. Of course it would need to remove the reference when the class was
deleted.
This seems like a major pain, so I think you need to decide if this is
absolutely neccessary.
What does each instance of the class have to do when MajorMode changes?
Can't each instance do the update the next time one of their functions are
called? If so, I would keep a non static variable int LastMajorMode inside
the class and compare it with MajorMode in calls. If it has changed, do
whatever updates you need to. The drawback of this approach is the
comparison of the two ints. I don't think that would actually take much
time though.