Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > shared member between class instances

Reply
Thread Tools

shared member between class instances

 
 
markww
Guest
Posts: n/a
 
      08-25-2006
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!

 
Reply With Quote
 
 
 
 
Steve Pope
Guest
Posts: n/a
 
      08-25-2006
markww <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
markww
Guest
Posts: n/a
 
      08-25-2006

Steve Pope wrote:
> markww <(E-Mail Removed)> 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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      08-25-2006

markww wrote:
> Steve Pope wrote:
> > markww <(E-Mail Removed)> 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

 
Reply With Quote
 
Steve Pope
Guest
Posts: n/a
 
      08-25-2006
markww <(E-Mail Removed)> 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
 
Reply With Quote
 
markww
Guest
Posts: n/a
 
      08-25-2006

Steve Pope wrote:
> markww <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
share function argument between subsequent calls but not between class instances! K. Jansma Python 7 02-21-2006 01:31 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM
Differences between "class::member" & "object.member" Jian H. Li C++ 4 02-13-2004 10:54 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments