Reputation: 7574
In the following example -
#include <iostream>
int someMethod(){
static int a = 20;
static const int result = a + 1;
++a;
std::cout << " [" << a << "] ";
return result;
}
int main(){
std::cout << someMethod() << "\n";
std::cout << someMethod() << "\n";
std::cout << someMethod() << "\n";
}
The output comes as -
[21] 21
[22] 21
[23] 21
What is the reason that is preventing result
value to be modified on subsequent calls made to the same function? I've printed the output of variable a
as well, which is certainly being incremented and since it is static as well there must not be multiple copies existing for the same method.
Upvotes: 7
Views: 659
Reputation: 845
Static is a hint to the compiler not to reinitialize a variable aside from forcing the compiler to allocate the variable's value on the program's data segment.
Upvotes: 1
Reputation: 66200
As explained by others, result
is a static
variable so it's initialized only at the first execution of someMethod()
.
And result
is also const
, so the first value assigned with
static const int result = a + 1;
remain unmodified for the following execution of the program.
I suppose you were expecting something that is achievable using a reference; if you modify the preceding line as follows
static const int & result = a;
^
// note the & ---|
you link result
to a
and modifying a
you modify result
and the output is
[21] 21
[22] 22
[23] 23
The problem is that you can reference a variable to another variable, not to an expression; so you can link result
to a
, not to a+1
(not to the dynamic value of a+1
); so the following line
static const int & result = a + 1;
compile but (if I'm not wrong) link result
to the unnamed variable where is stored (at the first execution of someMethod()
) the result of the expression a + 1
, so the output is again
[21] 21
[22] 21
[23] 21
Upvotes: 0
Reputation: 148890
The const
here only disturb the reader from the real cause. This code performs exactly the same.
int someMethod(){
static int a = 20;
static int result = a + 1;
++a;
std::cout << " [" << a << "] ";
return result;
}
The real reason is that the =
sign can represent two different operations in C++: an assignation (executed each time) or an initialisation (executed only when the variable is created). The difference in when it is part of declaration/definition of the variable.
In normal context (not bloc static variables), both are equivalent because an automatic variable is created every time the block in which it is declared it run (or at least the compiler must ensure that all behaves as if it that the case).
But for a block static variable the initialization occurs only once and here the variable result is initialized to 21 and its value will never change.
Those variants would be much different
int someMethod(){
static int a = 20;
static int result;
result = a + 1; // assignation: result will see its value change with the value of a
...
int someMethod(){
static int a = 20;
static const int result = a;
result = a + 1; // Error modification of a const declared variable
Upvotes: 4
Reputation: 7542
Since result
is static, it will be initialized only once during runtime.Therefore the following line is executed only once no matter how many times you call someMethod()
static const int result = a + 1;
Upvotes: 4