Hi guys, I have a class that contains a protected variable, a virtual function, and two other classes that inherit the base class,my problem is that these two derived classes has to use the the same variable which is found in the base class and they have to update it. for example if derived class1 updates the variable in the base class, derived class22 has to see the changes.
Hmmm....
Hmmmm....
OK, so that's assuming that if I had an object of base, an object of derived 1 and an object of derived 2, any of those objects updating that base member would have to see the changes as a group?
OK, that's not going to be easy... I can't say that I know an easy workaround. Static-ness does not carry over inheritance, I think (so if you had a base static member, there would be a new static version for the derived classes). You could create a static pointer member in the base class then refer back to the desired member. That way, you can control the member as though it was in base, but all the classes can refer to it.
I'm sorry but that's the best I can think.
You don't know what static means as a member?
OK, when you specify a member as static, it means that the owning class (and all those derived from it) all share exactly one copy of the member. No matter how many of them you create, the static member is the same between all of them. It's literally one variable bound to all the objects. Therefore, any changes in one copy of the static member will become apparent in the others immediately, because they're all actually the same.
Because the derived classes are both derived from the same base, giving the base member a static specifier will render that member static across the base and all its children. Hence, it works.