468,272 Members | 2,194 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

communicating with every object of the class

hey all,

i've got a class that A that has a static class member

static int MajorMode;

it's important that if one instance of the class changes this variable, all
instances react immediately. while they'll all see the new value of the
variable, how do you get them each to execute a function when MajorMode
changes?

thanks -- lou


Feb 8 '06 #1
9 1178
"lou zion" <il********@adelphia.net> wrote in
news:Jf******************************@adelphia.com :
hey all,

i've got a class that A that has a static class member

static int MajorMode;

it's important that if one instance of the class changes this
variable, all instances react immediately. while they'll all see the
new value of the variable, how do you get them each to execute a
function when MajorMode changes?


You'd have to define what "react immediately" means. I suppose one
Standards-friendly way to implement what I think you want:

1) Have some static factory methods for creating instances of the object.
These instances are stored in some sort of container.
2) Have a static method for modifying MajorMode.
3) When that static method is invoked, that method can then iterate over
the container of objects and invoke some function on each instance.
4) Have some static sink methods for destroying instances of the object.
This would also be responsible for removing the instances from the
aforementioned container.
Feb 8 '06 #2
"lou zion" <il********@adelphia.net> wrote in message
news:Jf******************************@adelphia.com ...
hey all,

i've got a class that A that has a static class member

static int MajorMode;

it's important that if one instance of the class changes this variable,
all instances react immediately. while they'll all see the new value of
the variable, how do you get them each to execute a function when
MajorMode changes?

thanks -- lou


That's where wrappers come in. MajorMode should be private, and anything
that changes it should have to go through interfaces, SetMajorMode or
GetMajorMode.

In your case SetMajorMode would need to, somehow, call a function on each
instance of your class. How you do that is the quandry. If all your
instances are in a vector, pass a reference to the vector to SetMajorMode.
Otherways would be to somehow keep references to each instance of your class
in a static vector, somehow, and iterate through that. Then you would only
create your class through a factory that also added a reference to the
vector. Of course it would need to remove the reference when the class was
deleted.

This seems like a major pain, so I think you need to decide if this is
absolutely neccessary.

What does each instance of the class have to do when MajorMode changes?
Can't each instance do the update the next time one of their functions are
called? If so, I would keep a non static variable int LastMajorMode inside
the class and compare it with MajorMode in calls. If it has changed, do
whatever updates you need to. The drawback of this approach is the
comparison of the two ints. I don't think that would actually take much
time though.

Feb 8 '06 #3
> 1) Have some static factory methods for creating instances of the object.
These instances are stored in some sort of container.
The object can register itself within the constructor. So no need using
a factory.
2) Have a static method for modifying MajorMode.
3) When that static method is invoked, that method can then iterate over
the container of objects and invoke some function on each instance.
4) Have some static sink methods for destroying instances of the object.


Again, the object can unplug itself within the destructor.

Ben
Feb 8 '06 #4

"benben" <be******@yahoo.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
1) Have some static factory methods for creating instances of the object.
These instances are stored in some sort of container.


The object can register itself within the constructor. So no need using a
factory.
2) Have a static method for modifying MajorMode.
3) When that static method is invoked, that method can then iterate over
the container of objects and invoke some function on each instance.
4) Have some static sink methods for destroying instances of the object.


Again, the object can unplug itself within the destructor.

Ben


Please explain how you would do this? Would you require the constructor to
be passed a reference to the registration list? Or some other way for the
class constructor to find the list?
Feb 8 '06 #5
Jim Langston wrote:

"benben" <be******@yahoo.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
1) Have some static factory methods for creating instances of the
object. These instances are stored in some sort of container.


The object can register itself within the constructor. So no need using a
factory.
2) Have a static method for modifying MajorMode.
3) When that static method is invoked, that method can then iterate over
the container of objects and invoke some function on each instance.
4) Have some static sink methods for destroying instances of the object.


Again, the object can unplug itself within the destructor.

Ben


Please explain how you would do this? Would you require the constructor
to
be passed a reference to the registration list? Or some other way for the
class constructor to find the list?


What about using a static variable?

#include <set>

class SelfRegister {

typedef std::set< SelfRegister * > Registry;

static Registry the_registry;
void enter ( void ) {
the_registry.insert( this );
}

void leave ( void ) {
the_registry.erase( this );
}

public:

SelfRegister ( void ) {
this->enter();
}

~SelfRegister ( void ) {
this->leave();
}

static
Registry::size_type count ( void ) {
return( the_registry.size() );
}

}; //SelfRegister

SelfRegister::Registry SelfRegister::the_registry;

#include <iostream>

int main ( void ) {
SelfRegister a;
std::cout << SelfRegister::count() << '\n';
{
SelfRegister b;
std::cout << SelfRegister::count() << '\n';
}
std::cout << SelfRegister::count() << '\n';
}

In a multithreaded environment, the methods enter() and leave() and, maybe,
count() would need to do some locking.
Best

Kai-Uwe Bux
Feb 8 '06 #6

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:ds**********@murdoch.acc.Virginia.EDU...
Jim Langston wrote:

"benben" <be******@yahoo.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
1) Have some static factory methods for creating instances of the
object. These instances are stored in some sort of container.

The object can register itself within the constructor. So no need using
a
factory.

2) Have a static method for modifying MajorMode.
3) When that static method is invoked, that method can then iterate
over
the container of objects and invoke some function on each instance.
4) Have some static sink methods for destroying instances of the
object.

Again, the object can unplug itself within the destructor.

Ben


Please explain how you would do this? Would you require the constructor
to
be passed a reference to the registration list? Or some other way for
the
class constructor to find the list?


What about using a static variable?

#include <set>

class SelfRegister {

typedef std::set< SelfRegister * > Registry;

static Registry the_registry;
void enter ( void ) {
the_registry.insert( this );
}

void leave ( void ) {
the_registry.erase( this );
}

public:

SelfRegister ( void ) {
this->enter();
}

~SelfRegister ( void ) {
this->leave();
}

static
Registry::size_type count ( void ) {
return( the_registry.size() );
}

}; //SelfRegister

SelfRegister::Registry SelfRegister::the_registry;

#include <iostream>

int main ( void ) {
SelfRegister a;
std::cout << SelfRegister::count() << '\n';
{
SelfRegister b;
std::cout << SelfRegister::count() << '\n';
}
std::cout << SelfRegister::count() << '\n';
}

In a multithreaded environment, the methods enter() and leave() and,
maybe,
count() would need to do some locking.
Best

Kai-Uwe Bux


This is interesting, and I like it a lot. One question though, why did you
use this->enter() and this->leave() instead of just enter() and leave()? Is
there some reason for this that I should be aware of, or just a style issue?
Feb 8 '06 #7
Jim Langston wrote:

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:ds**********@murdoch.acc.Virginia.EDU...
Jim Langston wrote:

"benben" <be******@yahoo.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
> 1) Have some static factory methods for creating instances of the
> object. These instances are stored in some sort of container.

The object can register itself within the constructor. So no need using
a
factory.

> 2) Have a static method for modifying MajorMode.
> 3) When that static method is invoked, that method can then iterate
> over
> the container of objects and invoke some function on each instance.
> 4) Have some static sink methods for destroying instances of the
> object.

Again, the object can unplug itself within the destructor.

Ben

Please explain how you would do this? Would you require the constructor
to
be passed a reference to the registration list? Or some other way for
the
class constructor to find the list?


What about using a static variable?

#include <set>

class SelfRegister {

typedef std::set< SelfRegister * > Registry;

static Registry the_registry;
void enter ( void ) {
the_registry.insert( this );
}

void leave ( void ) {
the_registry.erase( this );
}

public:

SelfRegister ( void ) {
this->enter();
}

~SelfRegister ( void ) {
this->leave();
}

static
Registry::size_type count ( void ) {
return( the_registry.size() );
}

}; //SelfRegister

SelfRegister::Registry SelfRegister::the_registry;

#include <iostream>

int main ( void ) {
SelfRegister a;
std::cout << SelfRegister::count() << '\n';
{
SelfRegister b;
std::cout << SelfRegister::count() << '\n';
}
std::cout << SelfRegister::count() << '\n';
}

In a multithreaded environment, the methods enter() and leave() and,
maybe,
count() would need to do some locking.
Best

Kai-Uwe Bux


This is interesting, and I like it a lot. One question though, why did
you
use this->enter() and this->leave() instead of just enter() and leave()?
Is there some reason for this that I should be aware of, or just a style
issue?


There is no reason to write this->enter(), except my habits: I am doing
mostly templated stuff, and in that context writing this-> very often helps
to disambiguate things.
Best

Kai-Uwe Bux
Feb 8 '06 #8
Kai-Uwe Bux wrote:
#include <set>

class SelfRegister {

typedef std::set< SelfRegister * > Registry;

static Registry the_registry;
void enter ( void ) {
the_registry.insert( this );
}

void leave ( void ) {
the_registry.erase( this );
}

public:

SelfRegister ( void ) {
this->enter();
}

~SelfRegister ( void ) {
this->leave();
}

static
Registry::size_type count ( void ) {
return( the_registry.size() );
}

}; //SelfRegister

SelfRegister::Registry SelfRegister::the_registry;

#include <iostream>

int main ( void ) {
SelfRegister a;
std::cout << SelfRegister::count() << '\n';
{
SelfRegister b;
std::cout << SelfRegister::count() << '\n';
}
std::cout << SelfRegister::count() << '\n';
SelfRegister c(a);
std::cout << SelfRegister::count() << '\n';
// Error: expected 2, got 1
}


You forgot the copy constructor. Either provide it or make it private.
Regards, Stephan

Feb 8 '06 #9
Stephan Brönnimann wrote:
Kai-Uwe Bux wrote:
#include <set>

class SelfRegister {

typedef std::set< SelfRegister * > Registry;

static Registry the_registry;
void enter ( void ) {
the_registry.insert( this );
}

void leave ( void ) {
the_registry.erase( this );
}

public:

SelfRegister ( void ) {
this->enter();
}

~SelfRegister ( void ) {
this->leave();
}

static
Registry::size_type count ( void ) {
return( the_registry.size() );
}

}; //SelfRegister

SelfRegister::Registry SelfRegister::the_registry;

#include <iostream>

int main ( void ) {
SelfRegister a;
std::cout << SelfRegister::count() << '\n';
{
SelfRegister b;
std::cout << SelfRegister::count() << '\n';
}
std::cout << SelfRegister::count() << '\n';


SelfRegister c(a);
std::cout << SelfRegister::count() << '\n';
// Error: expected 2, got 1
}


You forgot the copy constructor. Either provide it or make it private.
Regards, Stephan

Right. Thanks for catching that.

Kai-Uwe Bux
Feb 8 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Wendy Elizabeth | last post: by
2 posts views Thread by Jasper Jones | last post: by
1 post views Thread by David Veeneman | last post: by
2 posts views Thread by aaronj1335 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.