what is "static"? (question)
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Depending on where the variable is declared, the meaning of static changes.

In Pawn, if a variable is declared static in the global space then it functions exactly like a normal ("new") global variable, with the key exception that it is only visible in the file that it was declared in. This is useful for includes as the main gamemode file won't be able to access it, and other (include) files can use the same variable name without conflict.

If a variable is declared static within the local scope then it will be kept in memory and it will retain its value between function calls. Normal variables are immediately disposed of when the function ends. In that sense, a static local is useful when you require a global that is only used within that one function.
Ahh, ok for example about static inside functions i do this:

Код:
stock function()
{
     static a =50;
     a++;
     print("%d", a);
}

function();
function();
it will add 1 to 50 and it becomes 51 and the number 51 will be kept as the new int for the value, right?

see i used "function();" two times, that means the first time it will return 50 + 1 = 51 and then, it will return the second time 52 because it kept "51" and it added 1 after
am i right?

and one little question, can we use static for strings?
thanks in advance
Reply


Messages In This Thread
what is "static"? (question) - by okaym8 - 26.11.2015, 10:31
Re: what is "static"? (question) - by Vince - 26.11.2015, 11:06
Re: what is "static"? (question) - by okaym8 - 26.11.2015, 15:24
Re: what is "static"? (question) - by vassilis - 26.11.2015, 15:33
Re: what is "static"? (question) - by okaym8 - 26.11.2015, 15:35

Forum Jump:


Users browsing this thread: 1 Guest(s)