I noticed that if you initialize a static variable in C++ in code, the initialization only runs the first time you run the function.
That is cool, but how is that implemented? Does it translate to some kind of twisted if statement? (if given a value, then ..)
void go( int x ) { static int j = x ; cout << ++j << endl ; // see 6, 7, 8 } int main() { go( 5 ) ; go( 5 ) ; go( 5 ) ; }
That depends on what staticA actually represents. If it is some kind of singleton, that should only exist once in any program, then the easy solution is make it so that there is only a single instance of staticA . And a way to do that is to require that any program that uses libB.so also links against libA.
Static variables can be assigned as many times as you wish. static int count = 0; is initialization and that happens only once, no matter how many times you call demo .
Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.
You can define a static field using the static keyword. If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.
Yes, it does normally translate into an implicit if
statement with an internal boolean flag. So, in the most basic implementation your declaration normally translates into something like
void go( int x ) { static int j; static bool j_initialized; if (!j_initialized) { j = x; j_initialized = true; } ... }
On top of that, if your static object has a non-trivial destructor, the language has to obey another rule: such static objects have to be destructed in the reverse order of their construction. Since the construction order is only known at run-time, the destruction order becomes defined at run-time as well. So, every time you construct a local static object with non-trivial destructor, the program has to register it in some kind of linear container, which it will later use to destruct these objects in proper order.
Needless to say, the actual details depend on implementation.
It is worth adding that when it comes to static objects of "primitive" types (like int
in your example) initialized with compile-time constants, the compiler is free to initialize that object at startup. You will never notice the difference. However, if you take a more complicated example with a "non-primitive" object
void go( int x ) { static std::string s = "Hello World!"; ...
then the above approach with if
is what you should expect to find in the generated code even when the object is initialized with a compile-time constant.
In your case the initializer is not known at compile time, which means that the compiler has to delay the initialization and use that implicit if
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With