static 成员必须要在类外重新定义一遍,否则【出现无法识别的标示符错误】
实例
template <class T>
class My
{
public:
static T cnt;
My()
{
cnt = cnt + 1;
cout << cnt << endl;
}
};
template <class _Type>
_Type My<_Type>::cnt = 0;//初始化静态变量
int main()
{
My<int> m1; // 1
My<int> m2; // 2
My<int> m3; // 3
}
参考: