C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

variable

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
sunnewton
Guest





PostPosted: Sat Oct 29, 2005 12:29 pm    Post subject: variable Reply with 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:)

Back to top
osmium
Guest





PostPosted: Sat Oct 29, 2005 12:44 pm    Post subject: Re: variable Reply with quote



"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





PostPosted: Sat Oct 29, 2005 12:47 pm    Post subject: Re: variable Reply with quote



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





PostPosted: Sat Oct 29, 2005 12:56 pm    Post subject: Re: variable Reply with quote

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





PostPosted: Sat Oct 29, 2005 1:02 pm    Post subject: Re: variable Reply with quote

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





PostPosted: Sat Oct 29, 2005 5:42 pm    Post subject: Re: variable Reply with quote

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





PostPosted: Sat Oct 29, 2005 8:45 pm    Post subject: Re: variable Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.