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

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.