Non-static member update static data

Obviously, a non-static member function can read static data. In fact, this is an important point to have static data - so that member functions of the instance can read it.

But is there a good reason (from an OOP design perspective) to have non-static member functions UPDATE a static data variable?

I know of a trivial example of how we can keep a counter of the number of instances that we have created for a particular object. This way we can have a constructor that updates the static int counter. Thus

class Foo
{
    static int ctr;

    Foo()
    {
        ctr++;
    }
}

      

But aside from this particular example, is there a common reason that static variables of non-static value get updated?

Personally, I think it sounds a little goofy, but I can't tell what exactly bothers me.

+3


source to share


2 answers


Grade. You might have workers that should throw out jobs if they are duplicated, and you can do this by putting a static set of non-CPU tasks to check.

The problem with this kind of static usage is that you can only have one such class per ... program. What if the owner of a larger program wants to run this class twice? Unfortunately. Or, what if another part of your program decides that it can actually use this task management system for something else? Unfortunately. Any small program can become part of a large program to save threads and other resources (I'm especially used to the Java context, where the JVM itself is very expensive).

So, when you feel like you have a need for this pattern, try creating an umbrella class to store candidate static data as non-static data, and have one umbrella class instance for many sub-instances. eg.

class Worker {
    static set<WorkItem*> inProcess; // the old way of doing it
    void work(WorkItem* w) {
        inProcess.add(w);
}

      



Should become

class Worker {
    Manager* manager; // shared between all instances, until your program grows
    void work(WorkItem* w) {
        manager->accept(w);
    }
}

class Manager {
    set<WorkItem> inProcess;
    void accept(WorkItem* w) {
        inProcess.add(w);
    }
}

      

This problem applies to any static, volatile data. But the problem becomes more and more perilous as data goes from read-only to read-write. Singleton is an anti-pattern .

+3


source


From the book

Thinking in C ++, Volume 1, Second Edition, Bruce Eckel, President, MindView, Inc.



Static members in C ++

There are times when you need one storage location that will be used by all objects in a class. In C, you have to use a global variable, but that is not very safe. Global data can be changed by anyone, and its name can clash with other identical names in a large project. It would be ideal if the data can be stored as if it were global, but hidden inside the class and clearly associated with that class. This is achieved with static members within the class. There is one piece of storage for a static data member, no matter how many objects this class you create. All objects have the same static storage space for this data item, so this is a way for them to "communicate" with each other. But static data belongs to the class;its name is scope within a class and can be public, private, or protected.

0


source







All Articles