title: C++ Decorator pattern
body: <p>I've a <em>vector</em> and several classes (located in separate files) to modify the one.<br>
  I want to have global access to the <code>std::vector</code>, but only within the derived classes
  when each call stores the result of previous and the last object should return the total result</p>
  
  <p>Could you explain how to build a high-performance interface using Decorator pattern with <code>std::vector</code>?<br>
  I may be wrong, and may need other pattern.</p>
  
  <pre><code>// A.h
  class A () {
        public : 
             vector&lt;int&gt; set(vector&lt;int&gt; &amp;vec);
  
              //return total result
             vector&lt;int&gt; get() {
                 return vector;
             }
  };
  
  // B.h
  class B () {
      //add new elements into vector
      //for example, add 2,3,4
  };
  
  // C.h
  class C () {
      //remove some elements from vector
      //for example, remove last element
  };
  
  //...
  
  // main.cpp
  #include "A.h"
  #include "B.h"
  #include "C.h"
  
  int main () {
  
      vector&lt;int&gt; set;
      set.push_back(1); //1
  
      C obj(new B(new A()));
      obj.set(set);
      obj.get(); // stores 1,2,3 (added by classes A, B, C)
  }
  </code></pre>
  
  <p>So, I don't want to do like this:</p>
  
  <pre><code>vector&lt;int&gt; set1;
  set1.push_back(1);
  
  A *A_init;
  A_init-&gt;set(set1); //add 1
  
  vector&lt;int&gt; set2 = A_init-&gt;get();
  
  B *B_init;
  B_init-&gt;set(set2); //add 2, stores 1,2
  
  vector&lt;int&gt; set3 = B_init-&gt;get();
  
  C *C_init;
  C_init-&gt;set(set3); //add 3, stores 1,2,3
  
  vector&lt;int&gt; set4 = C_init-&gt;get();
  
  /..
  </code></pre>
  
  <p>And I want to do like this:</p>
  
  <pre><code>vector&lt;int&gt; set;
  set.push_back(1);
  
  C obj(new B(new A()));
  obj.set(set);
  obj.get(); // stores 1,2,3
  </code></pre>
  
  <p>I've a simple impementation of a pattern Decorator.<br>
  But it is not quite what I need ((</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  class A {
      public:
          virtual void operation() = 0;
      };
  
  class Component : public A {
      public:
          virtual void operation() {
              std::cout&lt;&lt;"World!"&lt;&lt;std::endl;
          }
  };
  
  class B : public A {
      std::unique_ptr&lt;A&gt; add;
  
  public:
      B(A *component): add(component) {}
  
      virtual void operation() {
          std::cout &lt;&lt; ", ";
          add-&gt;operation();
      }
  };
  
  class C : public A {
      std::unique_ptr&lt;A&gt; add;
  
  public:
      C(A *component): add(component) {}
  
      virtual void operation() {
              std::cout &lt;&lt; "Hello";
              add-&gt;operation();
      }
  };
  
  int main() {
      C obj(new B(new Component()));
      obj.operation(); // prints "Hello, World!\n"
  
      return 0;
  }
  </code></pre>
  
  <p>PS:
  Sorry for not so clear explanation, because I don't know English so well</p>