473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1338
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Williams | last post by:
Hi, I am trying to create a VB6 application which can act as a 'connection broker' for other applications being run on the same machine. Essentially, the broker app will setup a number of...
2
by: Wendy Elizabeth | last post by:
I am new to using .net. I am told that I need to setup a config file that will be accessed by a VB.net dll. This vb.net dll will be setup to run every 5 to 10 minutes by a timer that is coded into...
6
by: PaulN | last post by:
I need to instantiate a class (Class1) in the startup form (Form1), set some of Class1's properties then open another form (Form2) and have it read Class1's properties and set the remainder of...
6
by: Joe Jax | last post by:
I have an object that spawns a worker thread to process one of its methods. That method processes methods on a collection of other objects. During this processing, a user may request to cancel the...
2
by: Jasper Jones | last post by:
I have a main form which has a list of items from a table on my database. If I click one of these items it brings up a second form which lets you edit the details for that item. If I change an...
1
by: David Veeneman | last post by:
This posting is for the Google crawler and requires no response. I have created a multi-level object hierarchy, in which the root object needs to be notified whenever any property of any object...
1
by: shettync | last post by:
Hi, In one of the projects that I am working on, have been sending xml messages back n forth btw the client(swing) and the corba server. The serialized objects(in xml representation) are rich...
6
by: dolulob | last post by:
Hi, I'm trying to communicate with a console application through a c# program. the console application is micq a console based ICQ client. I want to be able to send an receive messages through...
2
by: aaronj1335 | last post by:
I am trying to write an application that controls a robot via a serial port. I made a robot class that includes the serial port over which I send commands, and I would like to be able to have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.