1

I am new to C++ and currently reading Bjarne Stroustrup's "Principle and Practice using C++". In chapter 8.6.2 about why global variables are bad, he gives the following example which can solve the problem of static initialization fiasco:

const Date& default_date()
{
     static const Date dd(1970,1,1);        // initialize dd first time we get here
     return dd;
} 

The static local variable is initialized (constructed) only the first time its function is called. Note that we returned a reference to eliminate unnecessary copying and, in particular, we returned a const reference to prevent the calling function from accidentally changing the value.

Some time after, I was reading in this StackOverflow post about static data members in classes:

Static members exist before any object is created, that is what makes them static. Therefore their initialization does not happen on first instance, they are there already.

They are actually global variables within a class scope.

That said, you can use the inline modifier to initialize them inside the class without an out of class declaration.

From this, I take it that static variables defined inside a function and class definition have different properties? For example, that the static variable inside the function is defined the first time the function is executed, while a static variable inside a class is defined before any objects are created/when class definition is read by the compiler.

Are there any more differences between the two?

3
  • 2
    "Are there any more differences between the two?..." Yes there are but this looks like a I need a comprehensive list which as per the above link is not suitable for this kind of Q&A site and are generally considered off-topic. I may be wrong here though. Commented May 22, 2024 at 14:22
  • 7
    The keyword static in C++ cannot be associated with its usage like this. The usages of static have nothing to do with each other. You have static class members, static variables in functions, static member functions, static variables declared at file scope, etc. etc. Each one is different from the other, so comparing and contrasting the usages doesn't make a lot of sense. Commented May 22, 2024 at 14:24
  • Tactical note: default_date solves the initialization order problem, but dd will be destroyed in reverse order to when it was created, and that might be a little harder to predict a little harder to get right. Still, this beats the crap out of the nigh-impossible to get right of the initialization order. Commented May 22, 2024 at 16:06

1 Answer 1

1

Are there any more differences between the two?

Yes, for one function static variable have local/block scope while class static member have class-scope.

That is, the difference is that they have different scopes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.