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 

shared member between class instances

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





PostPosted: Fri Aug 25, 2006 8:42 am    Post subject: shared member between class instances Reply with 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!
Back to top
Steve Pope
Guest





PostPosted: Fri Aug 25, 2006 8:42 am    Post subject: Re: shared member between class instances Reply with quote



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





PostPosted: Fri Aug 25, 2006 8:42 am    Post subject: Re: shared member between class instances Reply with quote



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





PostPosted: Fri Aug 25, 2006 8:51 am    Post subject: Re: shared member between class instances Reply with quote

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





PostPosted: Fri Aug 25, 2006 9:00 am    Post subject: Re: shared member between class instances Reply with quote

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





PostPosted: Fri Aug 25, 2006 9:05 am    Post subject: Re: shared member between class instances Reply with quote

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
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.