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