static variable:declare and define

Discussion in 'C++' started by linq936@hotmail.com, Apr 6, 2006.

  1. Guest

    Hi,
    I am confused at static variable in declaring and defining.

    I have the following code in header file,

    class c0;

    class c1 {
    static std::vector<c0> cs;
    };

    And in C file, I operate on c1::cs in some function. But VC7 compiler
    complains that c1::cs is not initialized.

    I have to add the following line to the top of the C code,
    std::vector<c0> c1::cs;
    Then it works.

    This makes half sense to me. Static variable must be defined in
    addition to declaring. But how come the above newly statement does the
    definition? Is that because std::vector does some dummy initialization?

    Then I have another header file,
    class MyString {...};

    static MyString myStr; // This variable does not belong to any
    class

    Then in the C file when I have
    myStr = "";
    The compiler complains that I am re-defining myStr.

    I wonder what is going on? Because myStr is a standalone variable, so
    its definition is defferent from class member?
     
    , Apr 6, 2006
    #1
    1. Advertising

  2. wrote:
    > I am confused at static variable in declaring and defining.
    >
    > I have the following code in header file,
    >
    > class c0;
    >
    > class c1 {
    > static std::vector<c0> cs;


    That's the declaration.

    > };
    >
    > And in C file, I operate on c1::cs in some function. But VC7 compiler
    > complains that c1::cs is not initialized.
    >
    > I have to add the following line to the top of the C code,
    > std::vector<c0> c1::cs;


    That's the definition.

    > Then it works.
    >
    > This makes half sense to me. Static variable must be defined in
    > addition to declaring.


    Static member variables must. And only if they are of non-integral type
    and are used outside the class itself.

    > But how come the above newly statement does the
    > definition?


    Because it's outside of the class, at the namespace level.

    > Is that because std::vector does some dummy
    > initialization?


    Nope. It's because the rules require it.

    > Then I have another header file,
    > class MyString {...};
    >
    > static MyString myStr; // This variable does not belong to any
    > class


    Then it's not a _member_, is it?

    > Then in the C file when I have
    > myStr = "";
    > The compiler complains that I am re-defining myStr.


    Huh? You must be including the header in more than one translation unit
    and that introduces multiple _definitions_ in your program.

    > I wonder what is going on? Because myStr is a standalone variable, so
    > its definition is defferent from class member?


    Yep. You got it!

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 6, 2006
    #2
    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. David T. Ashley
    Replies:
    6
    Views:
    542
    Tim Rentsch
    Dec 5, 2004
  2. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    700
    Victor Bazarov
    Aug 1, 2007
  3. ZelluX
    Replies:
    24
    Views:
    891
    Hendrik Maryns
    Nov 19, 2007
  4. PowerStudent
    Replies:
    3
    Views:
    269
    Alf P. Steinbach /Usenet
    Aug 6, 2010
  5. yoodavid
    Replies:
    64
    Views:
    603
    Tim Rentsch
    Oct 24, 2013
Loading...

Share This Page