fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <functional> // std::ref
  4. #include <vector>
  5.  
  6. class Foo
  7. {
  8. public:
  9. template <typename ...Ts>
  10. Foo(Ts&&... ts) : Foo(std::map<char, std::vector<int>&>{std::forward<Ts>(ts)...}) {}
  11.  
  12. Foo(std::vector<int> (&v)[26]) :
  13. A(v[0]),
  14. B(v[1])
  15. // and so on...
  16. {
  17. }
  18.  
  19. Foo(std::vector<int> &a, std::vector<int> &b) : A(a), B(b) // and so on...
  20. {
  21. }
  22.  
  23. Foo(const std::map<char, std::vector<int>&>& m) :
  24. A(m.at('A')),
  25. B(m.at('B'))
  26. // and so on...
  27. {
  28. }
  29. Foo(std::map<char, std::vector<int>&>&& m) :
  30. A(m.at('A')),
  31. B(m.at('B'))
  32. // and so on...
  33. {
  34. }
  35.  
  36. virtual void DoSomething()
  37. {
  38. A.push_back(42);
  39. std::cout << A[0] << std::endl;
  40. }
  41.  
  42. private:
  43. std::vector<int> &A, &B; //, &C, &D, &E, &F, &G; // and so on...
  44. };
  45.  
  46.  
  47. int main()
  48. {
  49. {
  50. std::vector<int> v[26];
  51.  
  52. Foo foo(
  53. std::make_pair('A', std::ref(v[0])),
  54. std::make_pair('B', std::ref(v[1]))
  55. // And so on...
  56. );
  57.  
  58. foo.DoSomething();
  59. std::cout << v[0][0] << std::endl;
  60. }
  61. {
  62. std::vector<int> v[26];
  63.  
  64. Foo foo(v);
  65.  
  66. foo.DoSomething();
  67. std::cout << v[0][0] << std::endl;
  68.  
  69. }
  70. {
  71. std::vector<int> v[26];
  72.  
  73. Foo foo(v[0], v[1]);
  74.  
  75. foo.DoSomething();
  76. std::cout << v[0][0] << std::endl;
  77.  
  78. }
  79. {
  80. std::vector<int> v[26];
  81. const std::map<char, std::vector<int>&> m {
  82. std::make_pair('A', std::ref(v[0])),
  83. std::make_pair('B', std::ref(v[1]))
  84. // And so on...
  85. };
  86. Foo foo(m);
  87.  
  88. foo.DoSomething();
  89. std::cout << v[0][0] << std::endl;
  90. }
  91. return 0;
  92. }
  93.  
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
42
42
42
42
42
42
42
42