#include <iostream>
using namespace std;
///////////////////////////////////////////
//模板类实例的静态成员数据
template<class Type>
class CList
{
Type *m_pHeader;
int m_Nodes;
public:
CList()
{
m_pHeader = NULL;
m_Nodes = 0;
}
static int m_ListValue;
};
class CNode{
CNode* m_pNext;
int m_data;
CNode()
{
m_pNext = NULL;
m_data = 0;
}
};
class CFlow{
CFlow* m_pNext;
char m_data;
CFlow()
{
m_pNext = NULL;
m_data = ' ';
}
};
//模板类的静态成员变量初始化
template<class Type>
int CList<Type>::m_ListValue = 10;
int _tmain(int argc, _TCHAR* argv[])
{
CList<CNode> listNode; //不定义对象也可以
cout<<"listNode的成员初始值是:"<<CList<CNode>::m_ListValue<<endl;
CList<CNode>::m_ListValue = 2018;
cout<<"listNode成员改变后值为:"<<CList<CNode>::m_ListValue<<endl;
CList<CFlow> listFlow;
cout<<"listFlow的成员初始值是:"<<CList<CFlow>::m_ListValue<<endl;
CList<CFlow>::m_ListValue = 2028;
cout<<"listFlow成员改变后值为:"<<CList<CFlow>::m_ListValue<<endl;
return 0;
}
运行结果:
对于同一类型的模板类实例,静态数据成员是共享的,不同节点类型的模板类其静态数据成员的值是不同的。