Static data members and templates (C++ only)

Each class template instantiation has its own copy of any static data members. The static declaration can be of template argument type or of any defined type.

You must separately define static members. The following example demonstrates this:

template <class T> class K
{
public:
      static T x;
};
template <class T> T K<T> ::x;

int main()
{
      K<int>::x = 0;
}
The statement template T K::x defines the static member of class K, while the statement in the main() function assigns a value to the data member for K <int>.

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]