title: C+11 Strategy Pattern with state
body: <p>An example of <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow"><strong>Strategy Pattern</strong></a> from the book, <a href="http://shop.oreilly.com/product/9780596007126.do" rel="nofollow"><strong>Head First Design Patterns</strong></a>, was written in C++ at <a href="http://r3dux.org/2011/07/an-example-implementation-of-the-strategy-design-pattern-in-c/" rel="nofollow">[<strong>here</strong>]</a>. I'm practicing to transform it into C++11 style according to <a href="http://www.infoq.com/presentations/gof-patterns-c-plus-plus-boost" rel="nofollow"><strong>Effective GoF Patterns with C++11 and Boost</strong></a> as showing below.</p>
  
  <p>The <strong>Quack</strong> behavior:</p>
  
  <pre><code>struct Quack {
      static void quack()
      {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
      }
  };
  
  struct MuteQuack {
      static void quack()
      {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
      }
  };
  </code></pre>
  
  <p>The <strong>Fly</strong> behavior:</p>
  
  <pre><code>struct FlyWithWings {
  public:
      static void fly()
      {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
      }
  };
  
  struct FlyNoWay {
  public:
      static void fly()
      {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
      }
  };
  </code></pre>
  
  <p>The <strong>Duck</strong> hierarchy:</p>
  
  <pre><code>class Duck
  {
  public:
      typedef std::function&lt;void(void)&gt; QUACK;
      typedef std::function&lt;void(void)&gt; FLY;
  
  public:
      Duck(const QUACK &amp;q, const FLY &amp;f)
          : m_Quack(q), m_Fly(f) {}
  
      virtual ~Duck()
      {
      }
  
      void perform_quack()
      {
          m_Quack();
      }
      void perform_fly()
      {
          m_Fly();
      }
  
  protected:
      QUACK   m_Quack;
      FLY     m_Fly;
  
  private:
      Duck(const Duck&amp;) = delete;
      Duck&amp; operator=(const Duck&amp;) = delete;
  };
  
  class MallardDuck
      : public Duck
  {
  public:
      MallardDuck()
          : Duck(&amp;Quack::quack, &amp;FlyWithWings::fly)
      {
      }
  };
  
  class PaintedDuck
      : public Duck
  {
  public:
      PaintedDuck()
          : Duck(&amp;MuteQuack::quack, &amp;FlyNoWay::fly)
      {
      }
  };
  </code></pre>
  
  <p>So far so good, the client works well.</p>
  
  <pre><code>int main()
  {
      MallardDuck x1;
      x1.perform_quack();
      x1.perform_fly();
  
      PaintedDuck x2;
      x2.perform_quack();
      x2.perform_fly();
  
      return 0;
  }
  </code></pre>
  
  <p>Now I would like to extend to a new class <code>RubberDuck</code> to <strong>Duck</strong> hierarchy, and the <code>RubberDuck</code> uses a new fly behavior <code>FlyWithRocket</code> which has a object state. As following:</p>
  
  <p>A new <strong>Fly</strong> behavior:</p>
  
  <pre><code>class FlyWithRocket {
  public:
      FlyWithRocket() : m_Energy(3) {}
      void fly()
      {
          if(m_Energy &gt; 0)
          {
              fly_with_rocket();
              --m_Energy;
          }
          else
          {
              fly_out_of_energy();
          }
      }
  
  private:
      void fly_with_rocket()
      {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
      }
      void fly_out_of_energy()
      {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
      }
  
      unsigned int m_Energy;
  };
  </code></pre>
  
  <p>A new <strong>Duck</strong> type:</p>
  
  <pre><code>class RubberDuck
      : public Duck
  {
  public:
      RubberDuck()
          : Duck(&amp;MuteQuack::quack, std::bind(&amp;FlyWithRocket::fly, std::ref(m_flyrocket)))
          , m_flyrocket()
      {
      }
  private:
      FlyWithRocket m_flyrocket;
  };
  </code></pre>
  
  <p>From now I'm wondering that the rule of the order of member initialization. The base <code>Duck</code> initializes before the member <code>m_flyrocket</code>, but note that the base <code>Duck</code> is initialized with binding <code>m_flyrocket</code> which is not initialized yet.
  As result as I run it in VS2013, this works without something wrong at run-time.</p>
  
  <p>But is the code actually not safe? If not, how could I modify to a better design?</p>