title: Chain of responsibility going back by pointers for no reason
body: <p>So I had a task to do a shop class, products class a basket class bla bla bla, I did all of this, tested, everything is working fine and that has nothing to do with the problem. Now what I had to do is create discounts and a chain of responsibility, like let's say I create 3 discounts and it goes through pointers and checks which one is suitable. So I did it, it does work, when I was debugging I checked, that it stops on the discount it needs to, returns me the price with the discount applied BUT THEN it somehow is going back by pointers and returning again what I don't need. I will make this more clear after I show the code and I will provide an example. So this is my <code>Discount.h</code> which holds a pointer to the next Discount and the main virtual <code>countDiscount();</code> function that plays the role in all of this.</p>
  <pre><code>#ifndef DISCOUNT_HEADER
  #define DISCOUNT_HEADER
  
  #include &quot;Basket.h&quot;
  
  class Discount
  {
  public:
      Discount():next(NULL){}
      virtual int countDiscount(Basket b) = 0;
      void nextDiscount(Discount* nextDisc) { next = nextDisc; }
  protected:
      Discount* next;
  };
  
  #endif
  </code></pre>
  <p>So, then I had to do a lot of discount types so I started with a fixated discount, let's say I have <code>FixatedDiscount(100, 15)</code>, so when the person's basket's contents are worth more than 100, then the discount is applied and it just takes 15 away from his total value. So if he shopped for like 110 then the discount applied and it did 110 - 15= 95. This is the <code>FixatedDiscount.h</code></p>
  <pre><code>#ifndef FIXATED_HEADER
  #define FIXATED_HEADER
  
  #include &quot;Discount.h&quot;
  
  class FixatedDiscount:public Discount
  {
  public:
      FixatedDiscount(int l, int d) : limit{ l }, discount{ d } {}
      int countDiscount(Basket b);
  private:
      int limit;
      int discount;
  };
  
  #endif
  </code></pre>
  <p>And this is <code>FixatedDiscount.cpp</code></p>
  <pre><code>#include &quot;FixatedDiscount.h&quot;
  #include &lt;iostream&gt;
  
  int FixatedDiscount::countDiscount(Basket b) {
      int totalPrice = b.countPrice(); //this method just counts the value of the basket which for our example is 120
      if (totalPrice &gt;= limit) {
          totalPrice -= discount;
          std::cout &lt;&lt; &quot;Handled when limit is &quot; &lt;&lt; limit &lt;&lt; &quot; and discount is: &quot; &lt;&lt; discount &lt;&lt; &quot; returning total price: &quot; &lt;&lt; totalPrice &lt;&lt; std::endl;
      }
      else if (next != NULL) {
          next-&gt;countDiscount(b);
      }
      else {
          std::cout &lt;&lt; &quot;I am the last handler&quot; &lt;&lt; std::endl;
      }
      return totalPrice;
  }
  </code></pre>
  <p>So let's say I had 3 discounts, <code>FixatedDiscount(150, 20);</code>, <code>FixatedDiscount(100, 15)</code>, <code>FixatedDiscount(50, 10)</code>. In my example I will make a basket that's worth 120, so it should skip the first discount and move on to the second one since he shopped more than 100 and the discount of 15 is applied but he didn't reach 150 to get discount of 20.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &quot;Product.h&quot;
  #include &quot;Basket.h&quot;
  #include &quot;Discount.h&quot;
  #include &quot;FixatedDiscount.h&quot;
  #include &lt;vector&gt;
  
  int main()
  {
      Product* p1 = new Product(&quot;banana&quot;, 20, &quot;fruit&quot;);
      Product* p2 = new Product(&quot;coconut&quot;, 35, &quot;fruit&quot;);
      Product* p3 = new Product(&quot;watermelon&quot;, 45, &quot;vegetable&quot;);
      Product* p4 = new Product(&quot;cucumber&quot;, 20, &quot;vegetable&quot;);
      Product* p5 = new Product(&quot;butter&quot;, 30, &quot;dairy&quot;);
      Product* p6 = new Product(&quot;milk&quot;, 40, &quot;dairy&quot;);
      std::vector&lt;Product*&gt; catalog{ p1, p2, p3, p4, p5, p6 };
  
      Basket basket;
      basket.insert(catalog[1]);
      basket.insert(catalog[2]);
      basket.insert(catalog[5]); //All of these 3 products make up to value of 120
  
      Discount* fixated1 = new FixatedDiscount(150, 20);
      Discount* fixated2 = new FixatedDiscount(100, 15);
      Discount* fixated3 = new FixatedDiscount(50, 10); //made these 3 discounts
      fixated1-&gt;nextDiscount(fixated2);
      fixated2-&gt;nextDiscount(fixated3); //linked by pointers these 3 discounts
      std::cout &lt;&lt; &quot;Price with the discount: &quot; &lt;&lt; fixated1-&gt;countDiscount(basket) &lt;&lt; std::endl;
  }
  </code></pre>
  <p>So what is going here is that as you saw in <code>FixatedDiscount.cpp</code> I made a simple <code>cout</code> to check which discount was reached, and it says <code>Handled when limit is 100 and discount is: 15, returning total price: 105</code> as it should be! BUT THEN, it somehow goes through the <code>countDiscount</code> function once again and returns <code>120</code>, which is the price without the discount applied, how is this possible? It does not go to the third discount, but somehow it goes through that function once more and returns 120, so my theory is that when it reached the second discount it returned me what I want which is <code>105</code> since a discount of 15 was applied, then it goes back to the first pointer which is the first discount, and since 120 is not more than 150 it does not apply the discount and returns me 120. Why doesn't it just return me what I want and stop? Why does it go through that function once more possibly going back by the pointer and applying again the discount that was already checked and shouldn't be checked anymore.</p>