I was wondering what would happen if we kinda "redefine"(that's not a good term for what is happening) a static variable.
#include <iostream>
void insert(int k) {
static int jo = k;
std::cout << jo << std::endl;
}
int main()
{
insert(1);
insert(2);
}//output 1 and 1
I expected that the code above would have a problem, but it runs without any. At least I would expect that the 2 of insert(2)
would go to some other memory location, but I couldn't find it. Although if we change it to:
#include <iostream>
int main()
{
static int jo = 1;
std::cout << jo << std::endl;
static int jo = 2;
std::cout << jo << std::endl;
}// 'jo' redefinition; multiple initialization
We get a problem, in this case I believe that's because the two declarations potentially conflict and one precedes the other what I think don't apply to the first code of this question. What happens in the first code?