title: Fluent interfaces and inheritance in C++
body: <p>I'd like to build a base (abstract) class (let's call it <code>type::base</code>) with some common funcionality and a fluent interface, the problem I'm facing is the return type of all those methods</p>
  
  <pre><code>  class base {
      public:
         base();
         virtual ~base();
  
         base&amp; with_foo();
         base&amp; with_bar();
      protected:
         // whatever...
    };
  </code></pre>
  
  <p>Now I could make subtypes, e.g.:</p>
  
  <pre><code>  class my_type : public base {
      public:
        myType();        
        // more methods...
    };
  </code></pre>
  
  <p>The problem comes when using those subtypes like this:</p>
  
  <pre><code> my_type build_my_type()
   {
      return my_type().with_foo().with_bar();
   }
  </code></pre>
  
  <p>This won't compile because we're returning base instead of my_type. </p>
  
  <p>I know that I could just:</p>
  
  <pre><code> my_type build_my_type()
   {
      my_type ret;
      ret.with_foo().with_bar();
  
      return ret;
   }
  </code></pre>
  
  <p>But I was thinking how can I implement it, and I've not found any valid ideas, some suggestion?</p>