| View previous topic :: View next topic |
| Author |
Message |
markww Guest
|
Posted: Fri Aug 25, 2006 8:42 am Post subject: shared member between class instances |
|
|
Hi,
I want all instance of a class type to share one variable, I forgot how
to do that:
class CMyClass {
public:
static int m_nSharedRes;
};
Now can all instances of CMyClass share that one variable, and see the
changes reflected in each instance? Like:
CMyClass a;
CMyClass b;
a.m_nSharedRes = 44;
// Now b can see it as 44 too.
cout << b.m_nSharedRes << endl; // should print 44;
Thanks! |
|
| Back to top |
|
 |
Steve Pope Guest
|
Posted: Fri Aug 25, 2006 8:42 am Post subject: Re: shared member between class instances |
|
|
markww <markww (AT) gmail (DOT) com> wrote:
| Quote: | Hi,
I want all instance of a class type to share one variable, I forgot how
to do that:
class CMyClass {
public:
static int m_nSharedRes;
};
Now can all instances of CMyClass share that one variable, and see the
changes reflected in each instance? Like:
CMyClass a;
CMyClass b;
a.m_nSharedRes = 44;
// Now b can see it as 44 too.
cout << b.m_nSharedRes << endl; // should print 44;
Thanks!
|
If you define it somewhere with
int CMyClass::m_nSharedRes;
it should work as you intended.
Steve |
|
| Back to top |
|
 |
Steve Pope Guest
|
Posted: Fri Aug 25, 2006 8:42 am Post subject: Re: shared member between class instances |
|
|
markww <markww (AT) gmail (DOT) com> wrote:
| Quote: | Steve Pope wrote:
class CMyClass {
public:
static int m_nSharedRes;
};
|
| Quote: | CMyClass a;
CMyClass b;
a.m_nSharedRes = 44;
// Now b can see it as 44 too.
cout << b.m_nSharedRes << endl; // should print 44;
If you define it somewhere with
int CMyClass::m_nSharedRes;
it should work as you intended.
So I just define it like:
class CMyClass {
public:
int CMyClass::m_nSharedRes;
};
??
|
The definition:
int CMyClass::m_nSharedRes;
needs to be outside of the declaration of the class. You need
to do this in addtion to the code in your first post. Remember
that the a class declaration by itself allocates no storage.
Steve |
|
| Back to top |
|
 |
markww Guest
|
Posted: Fri Aug 25, 2006 8:51 am Post subject: Re: shared member between class instances |
|
|
Steve Pope wrote:
| Quote: | markww <markww (AT) gmail (DOT) com> wrote:
Hi,
I want all instance of a class type to share one variable, I forgot how
to do that:
class CMyClass {
public:
static int m_nSharedRes;
};
Now can all instances of CMyClass share that one variable, and see the
changes reflected in each instance? Like:
CMyClass a;
CMyClass b;
a.m_nSharedRes = 44;
// Now b can see it as 44 too.
cout << b.m_nSharedRes << endl; // should print 44;
Thanks!
If you define it somewhere with
int CMyClass::m_nSharedRes;
it should work as you intended.
Steve
|
Hi Steve,
So I just define it like:
class CMyClass {
public:
int CMyClass::m_nSharedRes;
};
??
Thanks |
|
| Back to top |
|
 |
Greg Guest
|
Posted: Fri Aug 25, 2006 9:00 am Post subject: Re: shared member between class instances |
|
|
markww wrote:
| Quote: | Steve Pope wrote:
markww <markww (AT) gmail (DOT) com> wrote:
Hi,
I want all instance of a class type to share one variable, I forgot how
to do that:
class CMyClass {
public:
static int m_nSharedRes;
};
Now can all instances of CMyClass share that one variable, and see the
changes reflected in each instance? Like:
CMyClass a;
CMyClass b;
a.m_nSharedRes = 44;
// Now b can see it as 44 too.
cout << b.m_nSharedRes << endl; // should print 44;
Thanks!
If you define it somewhere with
int CMyClass::m_nSharedRes;
it should work as you intended.
Steve
Hi Steve,
So I just define it like:
class CMyClass {
public:
int CMyClass::m_nSharedRes;
};
??
|
No, m_nSharedRes must be declared static in order for each CMyClass
object to share the same variable.
Greg |
|
| Back to top |
|
 |
markww Guest
|
Posted: Fri Aug 25, 2006 9:05 am Post subject: Re: shared member between class instances |
|
|
Steve Pope wrote:
| Quote: | markww <markww (AT) gmail (DOT) com> wrote:
Steve Pope wrote:
class CMyClass {
public:
static int m_nSharedRes;
};
CMyClass a;
CMyClass b;
a.m_nSharedRes = 44;
// Now b can see it as 44 too.
cout << b.m_nSharedRes << endl; // should print 44;
If you define it somewhere with
int CMyClass::m_nSharedRes;
it should work as you intended.
So I just define it like:
class CMyClass {
public:
int CMyClass::m_nSharedRes;
};
??
The definition:
int CMyClass::m_nSharedRes;
needs to be outside of the declaration of the class. You need
to do this in addtion to the code in your first post. Remember
that the a class declaration by itself allocates no storage.
Steve
|
Ah yeah now I remember. Thanks guys,
Mark |
|
| Back to top |
|
 |
|