Guest User

Untitled

a guest
Aug 20th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. ////myheader.h
  2. #include <vector>
  3.  
  4. class interface {
  5. public:
  6. virtual int& getA() = 0;
  7. virtual int& getB() = 0;
  8. virtual int& getC() = 0;
  9. };
  10.  
  11. class interface_impl_Y : public interface {
  12. int *a, *b, *c;
  13. public:
  14. interface_impl_Y(int *aa, int *bb, int *cc) {
  15. a = aa;
  16. b = bb;
  17. c = cc;
  18. };
  19. int& getA() {return *a;};
  20. int& getB() {return *b;};
  21. int& getC() {return *c;};
  22. };
  23.  
  24. class interface_impl_X : public interface {
  25. int a = 0, b = 0, c = 0;
  26. public:
  27. interface_impl_X(int aa, int bb, int cc) {
  28. a = aa;
  29. b = bb;
  30. c = cc;
  31. };
  32. interface_impl_X(interface_impl_Y &y) {
  33. a = y.getA();
  34. b = y.getB();
  35. c = y.getC();
  36. };
  37. int& getA() {return a;};
  38. int& getB() {return b;};
  39. int& getC() {return c;};
  40. };
  41.  
  42. template<>
  43. class std::vector<interface> {
  44. public:
  45. vector<int> a, b, c;
  46. void push_back(interface &i) {
  47. a.push_back(i.getA());
  48. b.push_back(i.getB());
  49. c.push_back(i.getC());
  50. }
  51. interface& operator[](int i) {
  52. return *(new interface_impl_Y(&a[i], &b[i], &c[i]));
  53. }
  54. };
  55.  
  56. ////main.cpp
  57. #include <vector>
  58. #include "myheader.h"
  59.  
  60. using namespace std;
  61.  
  62. int main() {
  63. auto insert = interface_impl_X(1, 2, 3);
  64. vector<interface_impl_X> AoS;
  65. AoS.push_back(insert);
  66.  
  67. vector<interface> SoA;
  68. SoA.push_back(insert);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment