initialize static member variable

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

  1. markww

    markww Guest

    Hi,

    I put a static member variable in my class.

    class CMine {
    static int m_nCount;
    };

    How do I initialize it to zero? I can't do that in the constructor of
    the class can I? Won't that keep setting it to zero everytime a new
    instance is created?

    Thanks
    markww, Aug 26, 2006
    #1
    1. Advertising

  2. markww wrote:

    > Hi,
    >
    > I put a static member variable in my class.
    >
    > class CMine {
    > static int m_nCount;


    change the above line to:

    static int m_nCount = 0;

    > };


    Also, in a .cpp file somewhere, add the following line:

    int CMine::m_nCount;

    > How do I initialize it to zero?


    Do so in the class definition as shown above.

    > I can't do that in the constructor of
    > the class can I?


    No.

    > Won't that keep setting it to zero everytime a new
    > instance is created?


    Yep.

    Best regards,

    Tom
    Thomas Tutone, Aug 26, 2006
    #2
    1. Advertising

  3. * markww:
    > Hi,
    >
    > I put a static member variable in my class.
    >
    > class CMine {
    > static int m_nCount;
    > };
    >
    > How do I initialize it to zero?


    Like

    int CMine::m_nCount = 0;

    Or you could just declare it with no explicit initialization since
    static variables are zero-initialized before any other initialization
    (and there is no other initialization for an 'int' with no explicit
    initialization).

    By the way, I'd remove the Microsoft Hungarian notation prefixes. For
    the class the prefix adds more to write and reduces readability. For
    the static variable the prefix is directly misleading.


    > I can't do that in the constructor of the class can I?


    Technically you can, but that would in practice constrain the usage of
    the class, and it would be unexpected.


    > Won't that keep setting it to zero everytime a new instance is created?


    Yes.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 26, 2006
    #3
  4. * Thomas Tutone:
    > markww wrote:
    >
    >> Hi,
    >>
    >> I put a static member variable in my class.
    >>
    >> class CMine {
    >> static int m_nCount;

    >
    > change the above line to:
    >
    > static int m_nCount = 0;
    >
    >> };

    >
    > Also, in a .cpp file somewhere, add the following line:
    >
    > int CMine::m_nCount;
    >
    >> How do I initialize it to zero?

    >
    > Do so in the class definition as shown above.


    Comeau C/C++ 4.3.8 (Aug 19 2006 13:36:48) for ONLINE_EVALUATION_Alpha1
    Copyright 1988-2006 Comeau Computing. All rights reserved.
    MODE:strict errors C++

    "ComeauTest.c", line 2: error: data member initializer is not allowed
    Wild guess: Did you want static const and not just static?
    Wild guess: Your initializer is for a floating type (which is
    not allowed)
    static int m_nCount = 0;
    ^

    1 error detected in the compilation of "ComeauTest.c".



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 26, 2006
    #4
  5. Alf P. Steinbach wrote:

    > * Thomas Tutone:
    > > markww wrote:
    > >
    > >> Hi,
    > >>
    > >> I put a static member variable in my class.
    > >>
    > >> class CMine {
    > >> static int m_nCount;

    > >
    > > change the above line to:
    > >
    > > static int m_nCount = 0;
    > >
    > >> };

    > >
    > > Also, in a .cpp file somewhere, add the following line:
    > >
    > > int CMine::m_nCount;
    > >
    > >> How do I initialize it to zero?

    > >
    > > Do so in the class definition as shown above.

    >
    > Comeau C/C++ 4.3.8 (Aug 19 2006 13:36:48) for ONLINE_EVALUATION_Alpha1
    > Copyright 1988-2006 Comeau Computing. All rights reserved.
    > MODE:strict errors C++
    >
    > "ComeauTest.c", line 2: error: data member initializer is not allowed
    > Wild guess: Did you want static const and not just static?
    > Wild guess: Your initializer is for a floating type (which is
    > not allowed)
    > static int m_nCount = 0;
    > ^
    >
    > 1 error detected in the compilation of "ComeauTest.c".


    Ugh. Absolutely right - I was thinking of a static const int member.
    To the OP - follow Alf Steinbach's advice, and include the following
    line in a .cpp file:

    int CMine::m_nCount = 0;

    Don't make the change I indicated to the class definition.

    Sorry for the confusion.

    Best regards,

    Tom
    Thomas Tutone, Aug 26, 2006
    #5
  6. markww

    markww Guest

    Alf P. Steinbach wrote:
    > * markww:
    > > Hi,
    > >
    > > I put a static member variable in my class.
    > >
    > > class CMine {
    > > static int m_nCount;
    > > };
    > >
    > > How do I initialize it to zero?

    >
    > Like
    >
    > int CMine::m_nCount = 0;
    >
    > Or you could just declare it with no explicit initialization since
    > static variables are zero-initialized before any other initialization
    > (and there is no other initialization for an 'int' with no explicit
    > initialization).
    >
    > By the way, I'd remove the Microsoft Hungarian notation prefixes. For
    > the class the prefix adds more to write and reduces readability. For
    > the static variable the prefix is directly misleading.
    >
    >
    > > I can't do that in the constructor of the class can I?

    >
    > Technically you can, but that would in practice constrain the usage of
    > the class, and it would be unexpected.
    >
    >
    > > Won't that keep setting it to zero everytime a new instance is created?

    >
    > Yes.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    Ok looks good, its working as expected now, thanks guys,

    Mark
    markww, Aug 26, 2006
    #6
  7. markww

    Salt_Peter Guest

    markww wrote:
    > Hi,
    >
    > I put a static member variable in my class.
    >
    > class CMine {
    > static int m_nCount;
    > };
    >
    > How do I initialize it to zero? I can't do that in the constructor of
    > the class can I? Won't that keep setting it to zero everytime a new
    > instance is created?
    >
    > Thanks


    The member variable is static, so regardless of how many instances of
    CMine you have, doesn't it make sense that it needs to be initialized
    once outside of the class?

    #include <iostream>
    #include <ostream>

    class CMine
    {
    static int m_nCount;
    public:
    static int count() { return m_nCount; }
    };

    int CMine::m_nCount = 0; // or whatever value

    int main()
    {
    CMine cmine;
    std::cout << "count = " << cmine.count();
    std::cout << std::endl;

    return 0;
    }

    /*
    count = 0
    */
    Salt_Peter, Aug 26, 2006
    #7
    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. cppaddict
    Replies:
    16
    Views:
    19,775
    m_pahlevanzadeh
    Jul 11, 2012
  2. Jim Langston
    Replies:
    5
    Views:
    363
    Jim Langston
    Sep 23, 2005
  3. Replies:
    3
    Views:
    846
    Peter_Julian
    Oct 10, 2005
  4. Nan Li
    Replies:
    7
    Views:
    545
    Nan Li
    Nov 9, 2005
  5. dolphin
    Replies:
    3
    Views:
    1,341
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page