Using a static long variable : static « Language Basics « C++ Tutorial






#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;

long next();

int main() {
  for(int i = 0 ; i < 30 ; i++) {
    cout << std::setw(12) << next ();
  }
  cout << endl;
  return 0;
}

long next () {
  static long last = 0;
  last = last + 1;   
  return last;
}
1           2           3           4           5           6
   7           8           9          10          11          12          13
      14          15          16          17          18          19          20
          21          22          23          24          25          26
  27          28          29          30








1.12.static
1.12.1.Using a static long variable
1.12.2.Use static variable to compute a running average of numbers entered by the user
1.12.3.Initialize static member field outside the class declaration
1.12.4.A static local variable causes the compiler to create permanent storage for it in much the same way that it does for a global variable.