Hope this example helps to understand the difference between a static local and global variable.
#include <iostream> using namespace std; static int z = 0; void method1() { static int x = 0; cout << "X : " << ++x << ", Z : " << ++z << endl; } void method2() { int y = 0; cout << "Y : " << ++y << ", Z : " << ++z << endl; } int main() { method1(); method1(); method1(); method1(); method2(); method2(); method2(); method2(); return 0; }
exit:
X : 1, Z : 1 X : 2, Z : 2 X : 3, Z : 3 X : 4, Z : 4 Y : 1, Z : 5 Y : 1, Z : 6 Y : 1, Z : 7 Y : 1, Z : 8
emon
source share