#include<iostream>
using namespace std;
template<class T>
class Person{
public:
static int a;
};
//類外初始化
template<class T>int Person<T>::a = 0;
int main()
{
//類模板具體化爲int型
Person<int>ip1, ip2, ip3;
Person<char>cp1, cp2, cp3;
ip1.a = 10;
cp1.a = 100;
cout << ip1.a << " " << ip2.a << " " << ip2.a << endl;
cout << cp1.a << " " << cp2.a << " " << cp2.a << endl;
return 0;
}
運行結果:
10 10 10
100 100 100
這說明static成員是屬於類模板具體化的類,而不是類模板本身。