Safer (not better)
int f() { OnStart(); return(5); } const int i = f(); const int j_global = i; #define PRINT(A) Print(#A + " = " + (string)(A)) void OnStart() { static const int j_static = i; PRINT(j_static); PRINT(j_global); }
Result
j_static = 0 j_global = 0 j_static = 0 j_global = 5
It is necessary to understand well what potential problems can be when using static variables.
fxsaber: It is necessary to understand well what potential problems can be when using static variables.
You didn't show any. You showed that the order of initialization of global/static variables is undefined.
By the documentation they should only be initialized with constants and "= i;" is not one. IMO the compiler should reject any such code.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Which is better and safer?
use static variable in OnTick() function or global variable?