can someone help me understand what static does?
I'm a beginner in c++.
can someone help me understand what static does?
I'm a beginner in c++.
Please give a sample of the instance because it can mean different things.
for( int x=0 , static x=1 ; i<10 ; x++ )
for( int y=0 ; y < 10 ; y ++ )
cout<<x<<y<<endl;
>can someone help me understand what static does?
The static keyword has two functions in C++, when the object is in local scope to a function, static means that the object is not placed on the stack and has a lifespan equal to that of the program:
This code will flag a warning and most likely not work because the memory for p is released when the function returns. By declaring p as static however, the function will return the proper value:Code:char *function() { char p[] = "This is a test"; return p; }
The second usage of static is in a global scope, such as when you are declaring a function prototype. The problem with global scope is that everything has external linkage by default, meaning that other source files can access them and possibly have a naming conflict. By declaring them as static you are telling the compiler that they do not exist outside of this particular source file. It's a good practice in data hiding.Code:char *function() { static char p[] = "This is a test"; return p; }
-PreludeCode:// Only visible to this file static char *function() { // Resides in memory that is not released static char p[] = "This is a test"; return p; }
My best code is written with the delete key.
...and a third use of static is within a class; to specify that only one function/variable exists for all instances (and can be accessed/used when no individual object has been created).
Also, a static variable is only initialized ONCE. So if you had:
int foo()
{
static int i = 0; ///...only set to zero first call..
i++;
return i;
}
...call the function in a loop and it will print: 1, 2, 3, 4, 5, 6...etc.
whereas
int foo()
{
int i = 0;
i++;
return i;
}
...call THIS function in a loop and it will print: 1, 1, 1, 1, 1, ...etc.
There are many uses for static variables referenced in this way...
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
What I can't understand is why nobody seems to think there is anything bizarre about a language that has to use the same word to mean so many different things. Static is a fine name for any one of its 4 different meanings, but is there no limit?
Ok, I can go along with static member variables and static nonmember variables, but can anybody tell me why the global function thing needs to be called static. And BTW, how did static_cast get in there?
I think I'm going to start a Pascal society Al
People do think it's bizarre, which is why unnamed namespaces were invented; to prevent the need for one of its uses. But nobody seems to use them, and just tend to stick with static.What I can't understand is why nobody seems to think there is anything bizarre about a language that has to use the same word to mean so many different things.
Everyone thinks it's bizarre for the same keyword to have wildly different meanings based on context. If you find out why it's like this, let us know.What I can't understand is why nobody seems to think there is anything bizarre about a language that has to use the same word to mean so many different things. Static is a fine name for any one of its 4 different meanings, but is there no limit?
-Prelude
My best code is written with the delete key.