shared member between class instances

Discussion in 'C++' started by markww, Aug 25, 2006.

  1. markww

    markww Guest

    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!
     
    markww, Aug 25, 2006
    #1
    1. Advertising

  2. markww

    Steve Pope Guest

    markww <> 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
     
    Steve Pope, Aug 25, 2006
    #2
    1. Advertising

  3. markww

    markww Guest

    Steve Pope wrote:
    > markww <> 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
     
    markww, Aug 25, 2006
    #3
  4. markww

    Greg Guest

    markww wrote:
    > Steve Pope wrote:
    > > markww <> 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
     
    Greg, Aug 25, 2006
    #4
  5. markww

    Steve Pope Guest

    markww <> 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
     
    Steve Pope, Aug 25, 2006
    #5
  6. markww

    markww Guest

    Steve Pope wrote:
    > markww <> 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
     
    markww, Aug 25, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DJ Dev
    Replies:
    3
    Views:
    16,247
    Gandalf
    Feb 8, 2004
  2. E11
    Replies:
    1
    Views:
    4,958
    Thomas Weidenfeller
    Oct 12, 2005
  3. John Wohlbier
    Replies:
    2
    Views:
    399
    Josiah Carlson
    Feb 22, 2004
  4. K. Jansma
    Replies:
    7
    Views:
    339
    Duncan Booth
    Feb 21, 2006
  5. ardi
    Replies:
    2
    Views:
    225
Loading...

Share This Page