fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4. #include <vector>
  5.  
  6. struct IFactory {
  7. typedef std::shared_ptr<IFactory> SharedFactory;
  8. virtual std::string Name() { return "IFactory"; }
  9.  
  10. template<class... Arg>
  11. SharedFactory Dup(const Arg&... A) {
  12. return std::make_shared<std::remove_reference<decltype(*this)>::type>(A...);
  13. }
  14. enum {
  15. ClassA,
  16. ClassB,
  17. };
  18. };
  19.  
  20. class A :public IFactory {
  21. std::string Name() { return "A"; }
  22. bool Say() { std::cout << "Baw" << std::endl; }
  23. };
  24.  
  25. class B :public IFactory {
  26. std::string Name() { return "B"; }
  27. bool Say() { std::cout << "Maw" << std::endl; }
  28. };
  29.  
  30. typedef std::vector<IFactory::SharedFactory> FType;
  31.  
  32. FType MakeVector() {
  33. FType F = { std::make_shared<A>() ,std::make_shared<B>() };
  34.  
  35. return F;
  36. }
  37.  
  38. int main() {
  39. FType F = MakeVector();
  40.  
  41. auto A = F[0]->Dup();
  42.  
  43. std::cout << A->Name() << std::endl;
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 4188KB
stdin
Standard input is empty
stdout
IFactory