fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class A
  5. {
  6. private:
  7. T val1;
  8. T val2;
  9.  
  10. public:
  11. T getVal1()
  12. {
  13. return val1;
  14. }
  15. void setVal1(T aVal)
  16. {
  17. val1 = aVal;
  18. }
  19. T getVal2()
  20. {
  21. return val2;
  22. }
  23. void setVal2(T aVal)
  24. {
  25. val2 = aVal;
  26. }
  27. };
  28.  
  29. template <typename T>
  30. class B
  31. {
  32. private:
  33. A<T>* aPtr;
  34.  
  35. B(const B&); // TODO , disallow for now
  36. B& operator=(const B&); // TODO , disallow for now
  37.  
  38. public:
  39. B() : aPtr(new A<T>()) {}
  40. ~B() { delete aPtr; }
  41.  
  42. A<T>* getAPtr()
  43. {
  44. return aPtr;
  45. }
  46. T operator[](const int& key)
  47. {
  48. if(key == 0)
  49. {
  50. T res = getAPtr()->getVal1();
  51. return res;
  52. }
  53. else
  54. {
  55. T res = getAPtr()->getVal2();
  56. return res;
  57. }
  58. }
  59. };
  60.  
  61. int main()
  62. {
  63. B<int> foo;
  64. foo.getAPtr()->setVal1(1);
  65. int x = foo[0];
  66. std::cout << foo[0] << " " << x << std::endl; // 1 1
  67. }
  68.  
Success #stdin #stdout 0s 2856KB
stdin
Standard input is empty
stdout
1 1