Advertisement
codemedic

Using static variable along with templates

Mar 25th, 2011
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. //################ t1.h #################
  2. template <class T>
  3. struct C
  4. {
  5.     static int count;
  6. };
  7.  
  8. template <class T> int C<T>::count = 0;
  9.  
  10. //################ a.cpp #################
  11. #include "t1.h"
  12. #include <iostream>
  13.  
  14. void a_print()
  15. {
  16.     C<void>::count++;
  17.     std::cout << "a_print: " << C<void>::count << std::endl;
  18. }
  19.  
  20. //################ b.cpp #################
  21. #include "t1.h"
  22. #include <iostream>
  23.  
  24. void b_print()
  25. {
  26.     C<void>::count++;
  27.     std::cout << "b_print: " << C<void>::count << std::endl;
  28. }
  29.  
  30. //################ main.cpp #################
  31. void a_print();
  32. void b_print();
  33.  
  34. int main()
  35. {
  36.     a_print();
  37.     b_print();
  38. }
  39.  
  40. //################ Makefile #################
  41. all: test
  42.  
  43. test : main.o a.o b.o
  44.     g++ -o $@ $^
  45.  
  46. clean:
  47.     rm -rf *.o test
  48.  
  49. //################ OutPut #################
  50.  
  51. ~$ make
  52. g++    -c -o main.o main.cpp
  53. g++    -c -o a.o a.cpp
  54. g++    -c -o b.o b.cpp
  55.  
  56. ~$ ./test
  57. a_print: 1
  58. b_print: 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement