Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here it goes:</p> <pre><code>#include &lt;vector&gt; #include &lt;cstdio&gt; /* A class that operates on an std::vector. */ class Vector { public: virtual std::vector&lt;int&gt; &amp; getVector() = 0; virtual void operation() = 0; virtual ~Vector() {} }; /* Basic implementation of Vector. */ class BasicVector : public Vector { public: BasicVector(std::vector&lt;int&gt; &amp; refVector) : intVector(refVector) {} void operation() { intVector.push_back(2); } std::vector&lt;int&gt; &amp; getVector() { return intVector; } private: std::vector&lt;int&gt; &amp; intVector; }; /* First decorator. */ class VectorDecorator1 : public Vector { public: VectorDecorator1(Vector &amp; refVec) : intVec(refVec) {} std::vector&lt;int&gt; &amp; getVector() { return intVec.getVector(); } void operation() { intVec.operation(); intVec.getVector().push_back(3); } private: Vector &amp; intVec; }; /* Second decorator. Others can be easily added in the future. */ class VectorDecorator2 : public Vector { public: VectorDecorator2(Vector &amp; refVec) : intVec(refVec) {} std::vector&lt;int&gt; &amp; getVector() { return intVec.getVector(); } void operation() { intVec.operation(); intVec.getVector().push_back(4); } private: Vector &amp; intVec; }; int main(int argc, char *argv[]) { std::vector&lt;int&gt; my_vector; my_vector.push_back(1); BasicVector basic_vector(my_vector); VectorDecorator1 vd1(basic_vector); VectorDecorator2 vd2(vd1); vd2.operation(); for(unsigned i = 0; i &lt; my_vector.size(); i++) { printf("%d\n", my_vector[i]); } return 0; } </code></pre> <p>Note: if you prefer, it's easy to change BasicVector to make it operate on its own std::vector (instead of a reference).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload