Could anyone expain the "Static" to me
-
I checked msdn, and got this "Objects and variables declared as static retain their values for the duration of the program’s execution".... but still don't know what is exactly the difference between declaring "int a" and "static int a", what is the difference when using them?
-
I checked msdn, and got this "Objects and variables declared as static retain their values for the duration of the program’s execution".... but still don't know what is exactly the difference between declaring "int a" and "static int a", what is the difference when using them?
-
I checked msdn, and got this "Objects and variables declared as static retain their values for the duration of the program’s execution".... but still don't know what is exactly the difference between declaring "int a" and "static int a", what is the difference when using them?
int i your variable will be created when the execution reaches this line, and it will be destroyed when leaving the scope. static int i your variable will be created when the program starts (whereever the statement is placed) and will be destroyed when the program exits.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
int i your variable will be created when the execution reaches this line, and it will be destroyed when leaving the scope. static int i your variable will be created when the program starts (whereever the statement is placed) and will be destroyed when the program exits.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
again, I am still confusing... is that int i: it will be ready when you start use it static int i: it will be ready long before you use it So what's the use of the latter occasion? Since I don't use it, why should I care when it is created.
-
again, I am still confusing... is that int i: it will be ready when you start use it static int i: it will be ready long before you use it So what's the use of the latter occasion? Since I don't use it, why should I care when it is created.
static has another meaning. when you declare a variable local to a function as static, then is keeps its last value :
void foo() {
static int i = 0;
i++;
printf("%d\n", i);
}void main() {
for (int n = 0; n < 5; n++) {
foo();
}
}this prints :
1 2 3 4 5
another meaning is when you declare a class data member as static. this means that the member is shared between every instances of the class. this is useful when you want a member to be unique, like an instances counter, class constants, etc... the last meaning is when declaring a class member function as static. it will tell that the function is a class function ; it doesn't know a particuliar instance (nothis
pointer), it performs general operations...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
again, I am still confusing... is that int i: it will be ready when you start use it static int i: it will be ready long before you use it So what's the use of the latter occasion? Since I don't use it, why should I care when it is created.
bloodwinner wrote:
again, I am still confusing... is that int i: it will be ready when you start use it static int i: it will be ready long before you use it So what's the use of the latter occasion? Since I don't use it, why should I care when it is created.
Imagine a function that animates something on the screen. Each call to the function updates the phase of the animation by using a counter to determine what's displayed. To this end you need an int as the counter and you declare it inside the function rather than having your primary program pass the counter to the function. If you declare the int as non-static but you increment the int each time you enter the function the value of the int goes away because the variable was created on the stack when the function began and it was removed when the function ended. If you declare the int as static, whatever value it had on exit from the function is the value it will have when you call the function again because the variable was created before the program began execution and will not be destroyed until the program ends. Lilith
-
int i your variable will be created when the execution reaches this line, and it will be destroyed when leaving the scope. static int i your variable will be created when the program starts (whereever the statement is placed) and will be destroyed when the program exits.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
I checked msdn, and got this "Objects and variables declared as static retain their values for the duration of the program’s execution".... but still don't know what is exactly the difference between declaring "int a" and "static int a", what is the difference when using them?