title: Two members, default initialized in Base, possibly non-default initialized in Derived
body: <p>I have a base class with a default constructor.</p>
  
  <pre><code>class Base
  {
  public:
     Base();
  private:
     Type1 m_ofType1;
     Type2 m_ofType2;
  }
  
  Base::Base()
     : m_ofType1(defaultExpr1)
     , m_ofType2(defaultExpr2)
  {
  }
  </code></pre>
  
  <p>And I have a derived class, which may be:</p>
  
  <ul>
  <li>default constructed, with m_ofType1 and m_ofType2 being initialized by their respective default expressions,</li>
  <li>provided with a Type1 only, or</li>
  <li>provided with a Type2 only.</li>
  </ul>
  
  <pre><code>class Derived : public base
  {
  public:
     Derived(); // m_ofType1 and m_ofType2 will be initialized with their default expressions.
  
     Derived(Type1 overrideOfType1); // m_ofType1 will be initialized with overrideOfType1 and m_ofType2 will be initialized with its default expression.
  
     Derived(Type2 overrideOfType2); // m_ofType1 will be initialized with its default expression and m_ofType2 will be initialized with overrideOfType2.
  }
  </code></pre>
  
  <p>Base is for Production code and Derived is for test code.  I would like to implement the constructors without data or code duplication, but I can't see how.</p>
  
  <p>Given the constraints, which I hope have been made clear, do you all know how to implement the above constructors without data or code duplication?</p>
  
  <p>Example of code duplication:</p>
  
  <p>We could add protected constructors to base:</p>
  
  <pre><code>Base(Type1);
  Base(Type2);
  </code></pre>
  
  <p>That means that the Derived constructors would just forward the call to their respective Base constructors.</p>
  
  <pre><code>Derrived::Derrived()
     : Base()
  {
  }
  
  Derived::Derived(Type1 overrideOfType1)
     : Base(overrideOfType1)
  {
  }
  
  Derived::Derived(Type1 overrideOfType2)
     : Base(overrideOfType2)
  {
  }
  </code></pre>
  
  <p>In doing this, the question changes a little.  How do you implement the Base constructors without data or code duplication.</p>
  
  <p>Here's my best attempt at doing this.  It doesn't work, and I'll show you why.</p>
  
  <p>First, add another constructor to Base:</p>
  
  <pre><code>Base(Type1 ofType1, Type2 ofType2);
  
  </code></pre>
  
  <p>Base::Base()
     : Base(defaultExpr1, defaultExpr2)
  {
  }</p>
  
  <p>Base::Base(Type1 overrideOfType1)
     : Base(overrideOfType1, defaultExpr2)
  {
  }</p>
  
  <p>Base::Base(Type2 overrideOfType2)
     : Base(defaultExpr1, overrideOfType2)
  {
  }</p>
  
  <p>Base::Base(Type1 ofType1, Type1 ofType2)
     : m_ofType1(ofType1)
     , m_ofType2(ofType2)
  {
  }</p>
  
  <pre><code>
  You can see that defaultExpr1 and defaultExpr2 have been duplicated.
  </code></pre>