0

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

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by poliet
0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.