template<typename T>
void add()
{
static int count = 0;
count++;
}
template<typename T>
class XX
{
public:
inline static int count = 0;
void say()
{
count++;
}
};
//模板类同理
template<class T>
class templateClassUse {
private:
T x;
public:
static int testi;
templateClassUse()
{
cout << ++testi << endl;
}
};
template<class T>
int templateClassUse<T>::testi = 0; //类中静态对象的初始化
int main()
{
templateClassUse<int> a; //1
templateClassUse<int> b; //2
templateClassUse<char> c; //1
templateClassUse<char> d; //2
templateClassUse<char> e; //3
XX<int> dsss;
dsss.say(); //1
dsss.say(); //2
XX<string> dsss2;
dsss2.say(); //1
dsss2.say(); //2
add<int>(); //1
add<int>(); //2
add<char>(); //1
add<string>(); //1
add<string>(); //2
}
C++ 不同模板类型,不共享静态变量
最新推荐文章于 2023-01-07 21:38:57 发布