| View previous topic :: View next topic |
| Author |
Message |
sunnewton Guest
|
Posted: Sat Oct 29, 2005 12:29 pm Post subject: variable |
|
|
Hi,All,
A simple question:
How to share a variable between two classes? I mean hte variablein one
class updated then in the other it will update, too.
Thanks in advance:)
|
|
| Back to top |
|
 |
osmium Guest
|
Posted: Sat Oct 29, 2005 12:44 pm Post subject: Re: variable |
|
|
"sunnewton" writes:
| Quote: | A simple question:
How to share a variable between two classes? I mean hte variablein one
class updated then in the other it will update, too.
|
I think you should start by deciding what you want. Do you want to share a
variable? Or have two variables with the same value? You ask for both. I
can see serious problems in achieving the latter, see the deadlock problem.
|
|
| Back to top |
|
 |
nSurgeon Guest
|
Posted: Sat Oct 29, 2005 12:47 pm Post subject: Re: variable |
|
|
Add new base class with one static variable with type which You need
for this two classes. And if You change it over one class another will
see it automatically.
|
|
| Back to top |
|
 |
TIT Guest
|
Posted: Sat Oct 29, 2005 12:56 pm Post subject: Re: variable |
|
|
sunnewton sade:
| Quote: | Hi,All,
A simple question:
How to share a variable between two classes? I mean hte variablein one
class updated then in the other it will update, too.
Thanks in advance:)
|
Depends on what you actually want to accomplish with consideration
to a possible design.
#include <ostream>
class A {
public:
unsigned int d_i;
A():d_i(~0){}
};
class B {
public:
unsigned int & d_i;
B(unsigned int & i):d_i(i){}
};
#pragma argsused
int main(int argc, char* argv[])
{
A a;
B b(a.d_i);
std::cout<
a.d_i = 8;
std::cout<
b.d_i = 5;
std::cout<
return 0;
}
TIT
|
|
| Back to top |
|
 |
nSurgeon Guest
|
Posted: Sat Oct 29, 2005 1:02 pm Post subject: Re: variable |
|
|
Add new base class with one static variable with type which You need
for this two classes. And if You change it over one class another will
see it automatically.
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sat Oct 29, 2005 5:42 pm Post subject: Re: variable |
|
|
sunnewton wrote:
| Quote: | Hi,All,
A simple question:
How to share a variable between two classes? I mean hte variablein one
class updated then in the other it will update, too.
Thanks in advance:)
|
That's impossible, but the two objects (not classes) can both have a
pointer to the same piece of memory. When one object updates the memory
the other object will also see the change. This is a common reason for
using pointers.
john
|
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Sat Oct 29, 2005 8:45 pm Post subject: Re: variable |
|
|
"sunnewton" <sunshinenewton (AT) gmail (DOT) com> wrote
| Quote: | Hi,All,
A simple question:
How to share a variable between two classes? I mean hte variablein one
class updated then in the other it will update, too.
Thanks in advance:)
|
Depends on what you actually are trying to achieve. One way is to pass a
pointer/reference to the variable into one of the classes.
But, is yoru question about sharing a variable between two classes, or
between 2 instances of 2 different classes?
It comes down to: what are you trying to achieve? There are many ways, and
it depends on what you're trying to do.
|
|
| Back to top |
|
 |
|