fork download
  1. #include <iostream>
  2. using namespace std;
  3. using namespace std;
  4.  
  5. struct base
  6. {
  7. static int tCount;
  8. };
  9. int base::tCount = 0;
  10. template <class T>
  11. class Test : public base
  12. {
  13. private:
  14. T val;
  15. public:
  16. static int count;
  17. Test()
  18. {
  19. count++;
  20. tCount++;
  21. }
  22. // some other stuff in class
  23. };
  24.  
  25. template<class T>
  26. int Test<T>::count = 0;
  27.  
  28. int main()
  29. {
  30. Test<int> a; // value of count for Test<int> is 1 now
  31. Test<int> b;
  32. Test<int> i;// value of count for Test<int> is 2 now
  33. Test<double> c; // value of count for Test<double> is 1 now
  34. Test<char> d;
  35. Test<float> e;
  36. cout << Test<int>::count << endl; // prints 2
  37. cout << Test<double>::count << endl; //prints 1
  38. cout << base::tCount <<endl;
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
3
1
6