What's the difference between static and new?
#1

As the title says, anyone can explain me?
Reply
#2

read this, explains it all
Reply
#3

I think ****** once said:
"If you don't know the difference between 'new' and 'static', then don't use it"
Reply
#4

Quote:
Originally Posted by Threshold
Посмотреть сообщение
I think ****** once said:
"If you don't know the difference between 'new' and 'static', then don't use it"
Bullshit...

If he don't know the difference between "new" and "static" he ask. So, he did it.

The difference between "static" and "new" is, that the declaration of "static" can used also in filterscripts, includes and gamemodes at the same time.. The declaration of "new" can't used in filterscripts, includes and gamemodes at the same time.


- Mencent
Reply
#5

There are more diffrences...

static saves the pointer of an variable in the stack/heap.

For example:

PHP код:
stock func()
{
    static 
a;
    
printf("%d",++a);
}
//main()
func(); //It will print 1
func(); //It will print 2 
And if you use for example:

PHP код:
//in the include
static x;
//You can use in the main mode
new x//without problems 
Reply
#6

Oh ok, but 1 more question. I have an include called def.inc which has all of my arrays, enums, defines and other vars stored. If i create a connection handle for mysql in that include with static, it gives undefined symbol error, but if i create it with new, it works fine. Why is that?
Reply
#7

Because of the scope of static.

Read my post...read the post in the wiki...
Reply
#8

Quote:
Originally Posted by Mencent
Посмотреть сообщение
Bullshit...

If he don't know the difference between "new" and "static" he ask. So, he did it.

The difference between "static" and "new" is, that the declaration of "static" can used also in filterscripts, includes and gamemodes at the same time.. The declaration of "new" can't used in filterscripts, includes and gamemodes at the same time.


- Mencent
Okay, but that's not what I said. I said if you don't know what the difference is, don't use it. Also, 'new' can be used in filterscripts, includes and gamemodes at the same time as well? How is that a 'difference'?

Static variables remember their old values, and can only be used in the script in which they're declared.

Quote:
Originally Posted by Sjn
Посмотреть сообщение
Oh ok, but 1 more question. I have an include called def.inc which has all of my arrays, enums, defines and other vars stored. If i create a connection handle for mysql in that include with static, it gives undefined symbol error, but if i create it with new, it works fine. Why is that?
Because 'static' variables only exist within the script they are declared in. So if you put it in your include file, the variables will not work outside of that include file.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)