When should i use "static" over "new" variable?
In what situations should i use "static" over "new"? I dont get it.
Only when i dont need to empty/zero it first before using? |
Re: When should i use "static" over "new" variable?
Just search, there are literally 100 tutorials and questions about this.
|
Re: When should i use "static" over "new" variable?
new gets deleted in the end of the function, static is saved in the end of the function.
|
Re: When should i use "static" over "new" variable?
Quote:
But i still dont get it why would i use "new" over "static" and vice versa. If "static" is faster and does the same job as "new", why dont i use "static" all the times? Examples would be nice. |
Re: When should i use "static" over "new" variable?
Quote:
|
Re: When should i use "static" over "new" variable?
Use static for large arrays (at least like 500 or 1000 elements) unless the function declaring them is called rarely, or if you want a variable to preserve its value between function calls.
If at any time you spend at least a few seconds thinking whether you should go with static or new, just go with new, it won't matter. People are making a fuss about this as if the difference is noticeable, and also I've seen people slamming static over almost everything in their code which is just dumb. |
Re: When should i use "static" over "new" variable?
Quote:
Thank you. |
Re: When should i use "static" over "new" variable?
For example, we have client_prethink() which is executed every client frame.
If inside the function you use new var, you will have: 1. create var 2. use var 3. delete var (on func end) If you have static var instead: 1. create var (only first call) 2. use var (never delete on func end) |
Re: When should i use "static" over "new" variable?
Quote:
|
Re: When should i use "static" over "new" variable?
Quote:
Step 2: enjoy. |
All times are GMT -4. The time now is 06:29. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.