fork download
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // ClassTemplate.hpp
  3.  
  4. //#pragma once
  5.  
  6. #include <iostream>
  7.  
  8. template <typename T>
  9. class ClassTemplate
  10. {
  11. public:
  12. ClassTemplate()
  13. {
  14. std::cout << "ClassTemplate constructor" << std::endl;
  15. }
  16.  
  17. void Test()
  18. {
  19. std::cout << "ClassTemplate Test" << std::endl;
  20. }
  21. };
  22.  
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // a.hpp
  25.  
  26. //#pragma once
  27.  
  28. // declare that somewhere this will be defined
  29. extern ClassTemplate<int> obj; // NOT constructed here!
  30.  
  31. class A
  32. {
  33. public:
  34. A()
  35. {
  36. // use it
  37. obj.Test();
  38. }
  39. };
  40.  
  41. ///////////////////////////////////////////////////////////////////////////////
  42. // a.cpp
  43.  
  44. //#include "ClassTemplate.hpp"
  45. //#include "a.hpp"
  46.  
  47. // create object (point of instantiation)
  48. ClassTemplate<int> obj;
  49.  
  50. int main( int argc, char ** argv )
  51. {
  52. A a; // construct A, which uses obj
  53. }
  54.  
  55. ///////////////////////////////////////////////////////////////////////////////
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
ClassTemplate constructor
ClassTemplate Test