By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

variable

P: n/a
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:)

Oct 29 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"sunnewton" writes:
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.
Oct 29 '05 #2

P: n/a
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.

Oct 29 '05 #3

P: n/a
TIT
sunnewton sade:
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<<b.d_i<<std::endl; // prints ~0
a.d_i = 8;
std::cout<<b.d_i<<std::endl; // prints 8
b.d_i = 5;
std::cout<<a.d_i<<std::endl; // prints 5
return 0;
}

TIT
Oct 29 '05 #4

P: n/a
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.

Oct 29 '05 #5

P: n/a
sunnewton wrote:
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
Oct 29 '05 #6

P: n/a
"sunnewton" <su************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.
Oct 29 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.