initialize static variable in cpp file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: initialize static variable in cpp file

  1. #1
    Join Date
    Sep 2008
    Posts
    21

    initialize static variable in cpp file

    Hey guys, here is what I read in CProgramming.com:

    You cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file instead.
    Have two questions regarding to this:
    1. Kind of curious why it cannot be in header file. As long as the initialization does not happen inside the class should be fine right?
    2. Also, can initialization happen in a static member function of the class? If not why?

    Thank you so much!

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: initialize static variable in cpp file

    If you initialise the static member in the header file then it would exist as a different copy in every translation unit that included the header file. That is why it must be in the .cpp file. The only exception to this is a static member of a template class, that gets defined in the header file and its up to the linker to sort it out. This has to happen as a template definition in a .cpp file would be inaccessible.

    In answer to the second question the only statics that can be initialised within the class are static const primitives. For other class statics they would need be in the .cpp file. The constructors cant initialise statics as statics appear once per class and constructors initialise on a per object basis.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: initialize static variable in cpp file

    I'm not sure the second question was actually answered. Static members can never be "initialized" inside of a static member function. You can change them (if they aren't const) inside of static member functions but they have to be initialized prior to any static member functions being called. It doesn't make logical sense to allow "initialization" inside of a static member function because there is no guarantee as to which static member function would be called first. Object instances are created via constructors. In that case the constructor runs before any non-static member function so only constructors can initialize non-static members. You see the point? static members are shared by all object instances so there is no function or constructor that could initialize a static member. Therefore it must be done some other way.

  4. #4
    Join Date
    Sep 2008
    Posts
    21

    Re: initialize static variable in cpp file

    Quote Originally Posted by Russco View Post
    If you initialise the static member in the header file then it would exist as a different copy in every translation unit that included the header file. That is why it must be in the .cpp file. The only exception to this is a static member of a template class, that gets defined in the header file and its up to the linker to sort it out. This has to happen as a template definition in a .cpp file would be inaccessible.

    In answer to the second question the only statics that can be initialised within the class are static const primitives. For other class statics they would need be in the .cpp file. The constructors cant initialise statics as statics appear once per class and constructors initialise on a per object basis.
    I see Thank you very much!

  5. #5
    Join Date
    Sep 2008
    Posts
    21

    Re: initialize static variable in cpp file

    Quote Originally Posted by kempofighter View Post
    I'm not sure the second question was actually answered. Static members can never be "initialized" inside of a static member function. You can change them (if they aren't const) inside of static member functions but they have to be initialized prior to any static member functions being called. It doesn't make logical sense to allow "initialization" inside of a static member function because there is no guarantee as to which static member function would be called first. Object instances are created via constructors. In that case the constructor runs before any non-static member function so only constructors can initialize non-static members. You see the point? static members are shared by all object instances so there is no function or constructor that could initialize a static member. Therefore it must be done some other way.
    Thank you for the clear explanation. I learnt a lot

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    Re: initialize static variable in cpp file

    Quote Originally Posted by lqakane View Post
    You cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file instead.
    This is not entirely accurate. If the static data member is of a
    const integral or const enumeration type, its declaration in the class
    definition can specify a constant initializer. However, you must
    still DEFINE the variable.

    Example:

    Code:
    // in header file
    
    class MyClass
    {
       static const int Size = 10;
    
       int arr[Size];
    };
    
    
    // in cpp file
    
    const int MyClass::Size;  // must still be defined

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: initialize static variable in cpp file

    Quote Originally Posted by Philip Nicoletti View Post
    Code:
    // in header file
    
    class MyClass
    {
       static const int Size = 10;
    
       int arr[Size];
    };
    
    
    // in cpp file
    
    const int MyClass::Size;  // must still be defined
    Actually, this will produce an error:
    error LNK2005: "public: static int const MyClass::Size" (?Size@MyClass@@2HB) already defined in MyClass.obj

    Also, how did this thread shift to static const member declaration? The OP's question was about static member's INITIALIZATION!
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  8. #8
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,139

    Re: initialize static variable in cpp file

    Quote Originally Posted by VladimirF View Post
    Actually, this will produce an error:
    error LNK2005: "public: static int const MyClass::Size" (?Size@MyClass@@2HB) already defined in MyClass.obj
    Not if you ignore the part about putting
    Code:
    const int MyClass::Size;  // must still be defined
    inside the cpp file.

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    Re: initialize static variable in cpp file

    Quote Originally Posted by VladimirF View Post
    Actually, this will produce an error:
    error LNK2005: "public: static int const MyClass::Size" (?Size@MyClass@@2HB) already defined in MyClass.obj

    Also, how did this thread shift to static const member declaration? The OP's question was about static member's INITIALIZATION!
    1) As mentioned in previous post, the definition goes in the CPP file
    (as in my comment)

    2) My post did give initialization of the static member. It showed
    how it does not always have to be in the implementation file.

  10. #10
    Join Date
    Jan 2009
    Posts
    3

    Re: initialize static variable in cpp file

    Just remember:

    There can be only one!

    A static variable is created once and only once. Russco provided a good explanation of why this is the case.

  11. #11
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: initialize static variable in cpp file

    Quote Originally Posted by VladimirF View Post
    Also, how did this thread shift to static const member declaration? The OP's question was about static member's INITIALIZATION!
    The OP made an inaccurate statement that static member variables couldn't be initialized in the class declaration. Some of us were simply pointing out that the statement was incorrect and that they can be in limited cases. I think that all of the discussions here were relevant to the OP. You are correct about the linker error though. If the member is initialized in the header than you can't redefine it and reinitialize it in the .cpp as well.

  12. #12
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: initialize static variable in cpp file

    Quote Originally Posted by lqakane View Post
    Hey guys, here is what I read in CProgramming.com:

    Well what do you expect from CProgramming.com? I recommend these websites for learning a little c++.
    http://www.cplusplus.com/
    http://www.research.att.com/~bs/homepage.html
    http://www.parashift.com/c++-faq-lite/index.html

  13. #13
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: initialize static variable in cpp file

    Quote Originally Posted by PredicateNormative View Post
    Not if you ignore the part about putting
    Code:
    const int MyClass::Size;  // must still be defined
    inside the cpp file.
    Actually, I did what you've suggested.
    My mistake is - I am using MS extensions to C++ that makes out-of-class definition of static const members "optional".
    When these extensions are enabled, you'd get the error I've mentioned.
    Sorry for the confusion.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  14. #14
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    Re: initialize static variable in cpp file

    Quote Originally Posted by VladimirF
    Actually, this will produce an error:
    error LNK2005: "public: static int const MyClass::Size" (?Size@MyClass@@2HB) already defined in MyClass.obj
    I was not aware of that particular bug in the Visual C++ compiler.


    Quote Originally Posted by PredicateNormative View Post
    Not if you ignore the part about putting
    Code:
    const int MyClass::Size;  // must still be defined
    inside the cpp file. :D
    Of course, you can not ignore that as it is required by the standard.
    But it looks like that is the only choice for VC++ to get around the bug.

  15. #15
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,139

    Re: initialize static variable in cpp file

    Quote Originally Posted by Philip Nicoletti View Post
    Of course, you can not ignore that as it is required by the standard.


    I'm not saying that you're wrong, perhaps you are right, but I can't find this in the standard, or at least not in the 2005 draft that I have.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center