fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A {
  5. public:
  6. A() {}
  7. ~A() { cout << "A Destructor"; }
  8. };
  9.  
  10. template<typename Type>
  11. class TemplateTest {
  12. protected:
  13. Type* start;
  14. Type* end;
  15. Type* iter;
  16. public:
  17. void Generate(unsigned int count) {
  18. start = new Type[count];
  19. end = start + count;
  20. iter = start;
  21. }
  22. void DestroyAll() {
  23. for(; start < end; ++start) {
  24. (*start).~Type();
  25. }
  26. }
  27. void Push(Type val) {
  28. *iter = val;
  29. iter++;
  30. }
  31. };
  32.  
  33. int main() {
  34. cout << "Non-pointer test" << endl;
  35. TemplateTest<A> npt;
  36. npt.Generate(5);
  37. npt.DestroyAll();
  38.  
  39. cout << "\nPointer test";
  40. TemplateTest<A*> pt;
  41. pt.Generate(5);
  42. pt.Push(new A());
  43. pt.DestroyAll();
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 2816KB
stdin
Standard input is empty
stdout
Non-pointer test
A DestructorA DestructorA DestructorA DestructorA Destructor
Pointer test