Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • As a best practice, it is generally better to wrap your static variable in a static method (possibly as a local static) to avoid initialization order problems. Commented Apr 20, 2012 at 6:15
  • 2
    This rule is actually relaxed a bit in C++11. const static members usually don't have to be defined anymore. See: en.wikipedia.org/wiki/… Commented Apr 20, 2012 at 10:16
  • 4
    @afishwhoswimsaround: Specifying over generalized rules to all situations is not a good idea (best practices should be applied with context). Here you are trying to solve a problem that does not exist. The initialization order problem only affects object that have constructors and access other static storage duration objects. Since 'x' is int the first does not apply since 'x' is private the second does not apply. Thirdly this has nothing to do with the question. Commented Apr 20, 2012 at 11:07
  • 1
    Belongs on Stack Overflow? Commented Apr 24, 2013 at 11:13
  • 2
    C++17 allows inline initialization of static data members (even for non-integer types): inline static int x[] = {1, 2, 3};. See en.cppreference.com/w/cpp/language/static#Static_data_members Commented Feb 14, 2018 at 23:23