Each of the different types of template functions whose static variables are not shared
Examples are as follows:
Template<typename t>
void useofstatic (const t& A) {
static int i = 0;
cout << ++i << endl;
}
int main () {
useofstatic<int>);
Useofstatic<int> (a);
Useofstatic<double> (2.5);
System ("PAUSE");
return 0;
}
The output results are as follows:
1
2
1
A static variable for a class template is similar to a function template:
Template similar to
template<class t>
class Templateclassuse {
private:
T x;
Public:
static int testi;
Templateclassuse ()
{
cout << ++testi << endl;
}
;
Template<class t>
int templateclassuse<t>::testi = 0; Initialization of static objects in a class
Use:
Templateclassuse<int> A;
Templateclassuse<int> b;
Templateclassuse<char> C;
Templateclassuse<char> D;
Templateclassuse<char> e;
Output:
1
2
3
4
5
Conclusion: Different types of template classes or template functions do not share static variables