Confusion about static

Hi,
I'm gonna keep it short and straightforward because I'm sure I'm missing something really obvious and also I have plenty of more advanced stuff to learn but my stupid brain just doesn't let these small details go.
Basically what I don't understand in the code below is why doesn't 'x' go back to 10 when the function is called a second time by the 'cout'.


1
2
3
4
5
6
7
8
9
10
11
12
13
  int &fun() 
{ 
	static int x = 10; 
  	cout << x << endl;
	return x; 
  	
} 
int main() 
{ 
	fun() = 30; 
	cout << fun(); 
	return 0; 
} 

Last edited on
It seems to be a confusion about static and has nothing to do with references.

Does this also confuse you?
Except for the different scope of x, it's exactly the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int x = 10; 

int& fun()
{
    std::cout << x << '\n';
    return x;
}

int main()
{
    fun() = 30;
    std::cout << fun() << '\n';
}

It does cuz to me that doesn't look the same as the original code because x is initialised only once. Why is it not the same thing as this for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int x = 10; 

int& fun()
{
    x = 10;
    std::cout << x << '\n';
    return x;
}

int main()
{
    fun() = 30;
    std::cout << fun() << '\n';
}


Edit: Or at least why doesn't it produce a redefinition error (original code)?
Last edited on
I made a mistake. You're right that they are not "exactly the same except for scope" as I said above. But the only difference is when the initialization takes place.

The global one is initialized before main is called.
The static one is initialized the first time fun is called but never again afterwards.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct Obj {
    Obj(const char *msg) { std::cout << "Obj ctor: " << msg << '\n'; }
};

Obj glob("global");

void fun() {
    static Obj stat("static");
}

int main() {
    std::cout << "1\n";
    fun();
    std::cout << "2\n";
    fun();
    std::cout << "3\n";
}


Obj ctor: global
1
Obj ctor: static
2
3


If you need the static variable initialized every time, for some reason, then you could do this:

1
2
3
4
5
int& fun() {
    static int x;
    x = 10;
    // ...
}

Or if you needed to be able to initialize it occasionally, then you could do something like this:

1
2
3
4
5
int& fun(int init=-1) { // default is an unused value like 0, -1 or INT_MIN
    static int x = 0;
    if (init != -1) x = init;
    //...
}

Or maybe this:

1
2
3
4
5
int& fun(bool init=false) { // fun(true) initializes x back to 0
    static int x = 0;
    if (init) x = 0;
    //...
}

Last edited on
Ok, so why does this give an error:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

static int x = 0;
static int x = 1; 
static int x = 2;

int main(){

    return 0;
}


and this doesn't:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int& fun(){
    static int x = 1;
    cout << x << endl;
    return x;
}

int main() {
    for(int i = 0; i < 5; i++) {
        fun() = i;
    }
 
    return 0;
}
What does the compiler tell you?

In the first example you're redefining the same name (x) multiple times. Can't do that.

In the fun() function, there is only ever 1 int allocated.
Last edited on
Yea I get it now, all I needed was some sleep haha, thanks for the answers!
Topic archived. No new replies allowed.