title: C++ Builder pattern with Fluent interface
body: <p>I am trying to implement builder pattern with fluent interface for building the objects in C++. I want the builder to follow CRTP pattern.
  In Java, I would do something similar to the below code. How do I do the same in C++?</p>
  
  <p>The below is some java code that has a base class and a derived class. The derived class's builder inherits the base class's builder..</p>
  
  <pre><code>// Base class
  public abstract class BaseClass {
  
      private final int base_class_variable;
  
      BaseClass(final Builder &lt;?&gt; builder) {
          this.base_class_variable = builder.base_class_variable;
      }
  
      public abstract static class Builder &lt;B extends Builder&gt; {
  
          int base_class_variable;
  
          public B setBaseClassVariable(final int variable) {
              this.base_class_variable = variable;
              return self();
          }
  
          protected abstract B self();
      }
  
  }
  
  // Derived class
  public final class DerivedClass extends BaseClass {
  
      private final int derived_class_variable;
  
      private DerivedClass(final Builder builder) {
          super(builder);
          this.derived_class_variable = derived_class_variable;
      }
  
      public static Builder builder() {
          return new Builder();
      }
  
      public static final class Builder extends BaseClass.Builder &lt;Builder&gt; {
  
          private int derived_class_variable;
  
          public Builder setDerivedClassVariable(final int variable) {
              this.derived_class_variable = variable;
              return self();
          }
  
          public DerivedClass build() {
              return new DerivedClass(this);
          }
  
          @Override
          protected Builder self() {
              return this;
          }
      }
  }
  
  // Creating an instance of DerivedClass
  DerivedClass dInstance = DerivedClass.builder()
      .setBaseClassVariable(5)
      .setDerivedClassVariable(10)
      .build();
  </code></pre>