fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. using namespace std;
  5.  
  6. struct A { void what_type() { cout<<"A"<<endl; }};
  7. struct B { void what_type() { cout<<"B"<<endl; }};
  8. struct U { void what_type() { cout<<"U"<<endl; }};
  9. struct V { void what_type() { cout<<"V"<<endl; }};
  10.  
  11. class MyCommonAncestor {
  12. public:
  13. virtual void common_operation1()=0;
  14. virtual ~MyCommonAncestor() {}
  15. };
  16.  
  17. template <class X, class Y>
  18. class MyTemplateClass : public MyCommonAncestor {
  19. X myx;
  20. Y myY;
  21. public:
  22. void common_operation1() override { // addresses specific objects of type X and Y
  23. myx.what_type();
  24. myY.what_type();
  25. };
  26. X operation2(const Y& y) {};
  27. ~MyTemplateClass() { // to demonstrate automatic destruction for shared ptr
  28. cout<<"object destroyed"<<endl;
  29. }
  30. };
  31.  
  32. int main() {
  33. vector<shared_ptr<MyCommonAncestor>> myVector;
  34. myVector.push_back(make_shared<MyTemplateClass<A,B>>());
  35. myVector.push_back(make_shared<MyTemplateClass<U,V>>());
  36. for (auto& x: myVector)
  37. x->common_operation1();
  38. return 0;
  39. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
A
B
U
V
object destroyed
object destroyed