René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Static members of template classes in C++

#include <iostream>
using namespace std;

template <class T> class X {
  public:
    static int st_;
};

template <class T>
int X<T>::st_;

int main(int argc, char* argv[]) {

  X<int> x_int_1;
  X<int> x_int_2;

  X<char> x_char_1;
  X<char> x_char_2;

  x_int_1.st_=1;
  x_int_2.st_=2;

  x_char_1.st_=3;
  x_char_2.st_=4;

  cout << x_int_1.st_  << "," <<
          x_int_2.st_  << "," <<
          x_char_1.st_ << "," <<
          x_char_2.st_ << endl;


	return 0;
}
See also templates