Hello,

I have a class with as many objects I need. I am changing the value of some member variables individually here and there, however, now I want to be able to change one specific member variable of all objects e.g. bool visibility at once, so that all objects wll have visibility=OFF.

I thought of creating a vector of pointers to all objects, or an array of references and then making a static member function which does that by looping through all existing objects. But then again if I decide to have other member variables to change as well, I would need to have all sorts of funcs which do the trick for various member variables.

I have read ages ago there is an easy way out somehow.. Any help would be appreciated,

&Cheers,

poliet

Dani AI

Generated

' suggestion (a class-level/shared variable) is exactly the right tool when the property is truly global for every instance. In modern C++ prefer an inline variable in the header (C++17+) or a single out-of-class definition in a .cpp to satisfy the one-definition rule; protect concurrent access with std::atomic or a mutex if threads are involved (inline variables). Encapsulate the flag behind static setters/getters rather than exposing it publicly so state changes can be validated or trigger side effects.

If flexibility is required (multiple groups of objects, or the need to change many different members without writing a separate function for each), use a shared-config object or a registry + lambda approach. The registry keeps weak references to all live instances and provides a single apply operation that accepts any callable to mutate members safely. Example pattern:

class Widget {
    static std::vector<std::weak_ptr<Widget>> registry;
    Widget() = default;
public:
    static std::shared_ptr<Widget> create() {
        auto p = std::shared_ptr<Widget>(new Widget());
        registry.push_back(p);
        return p;
    }
    static void applyToAll(const std::function<void(Widget&)>& fn) {
        auto it = registry.begin();
        while (it != registry.end()) {
            if (auto sp = it->lock()) { fn(*sp); ++it; }
            else it = registry.erase(it);
        }
    }
    // instance members...
};

Practical notes: do not call shared_from_this() in a constructor; use a factory function as above. If objects must react (redraw, recalc), use an observer/callback so each instance can handle side effects. Choose the simplest pattern that matches the semantics: static for a single global flag, shared-config for grouped control, registry+lambda for flexible bulk updates. For details on weak pointers see std::weak_ptr.

Recommended Answers

All 2 Replies

A static data member?

class foo {
private:
	static bool vis;
	char ch;

public:
	void change_vis() { vis = !vis ;}
        //other member functions

};


bool foo::vis = true; //initialize the member

or

class foo {
private:
	static bool vis;
	char ch;
	friend void change_vis(foo &a );

public:
	//member functions
};

bool foo::vis = true;



void change_vis( foo &a )
{ 
	a.vis = !a.vis ;
}

Now you can call the change_vis( ) method from any of the objects of the class, and vis will be changed for all. Really, all the objects are accessing the same single data item.

Thanks! The easiest things are always the fartherst away in my mind.. :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.