Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. //// myheader.cpp
  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_X : public interface {
  12.   int a = 0, b = 0, c = 0;
  13. public:
  14.   interface_impl_X(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_Y : public interface {
  25.   int *a, *b, *c;
  26. public:
  27.   interface_impl_Y(int *aa, int *bb, int *cc) {
  28.     a = aa;
  29.     b = bb;
  30.     c = cc;
  31.   };
  32.   int& getA() {return *a;};
  33.   int& getB() {return *b;};
  34.   int& getC() {return *c;};
  35. };
  36.  
  37. template<>
  38. class std::vector<interface> {
  39. public:
  40.   vector<int> a, b, c;
  41.   void push_back(interface &i) {
  42.     a.push_back(i.getA());
  43.     b.push_back(i.getB());
  44.     c.push_back(i.getC());
  45.   }
  46.   interface& operator[](int i) {
  47.     return *(new interface_impl_Y(&a[i], &b[i], &c[i]));
  48.   }
  49. };
  50.  
  51.  
  52. //// main.cpp
  53. #include <vector>
  54. #include "myheader.h"
  55.  
  56. using namespace std;
  57.  
  58. int main() {
  59.   auto insert = interface_impl_X(1, 2, 3);
  60.   vector<interface_impl_X> AoS;
  61.   AoS.push_back(insert);
  62.  
  63.   vector<interface> SoA;
  64.   SoA.push_back(insert);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement