title: How to call a C++ class Constructor from another Constructor
body: <p>I'm trying to create an object in C++ that requires multiple object constructors. Say <code>Foo()</code> and <code>Foo(int)</code> where <code>Foo(int)</code> then calls <code>Foo()</code>. The simplified code is written below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  class Foo{
  
  private:
          int iX; 
  
  public:
          void printX(string sLabel){
              cout &lt;&lt; sLabel &lt;&lt; " : " &lt;&lt; " Foo::iX = " &lt;&lt; Foo::iX &lt;&lt; endl;
          };  
          void setX(int iX){
              Foo::iX = iX; 
              Foo::printX("setX(void) Method");
          };  
          Foo(){
              Foo::iX = 1;
              Foo::printX("Foo(void) Constructor");
          };
          Foo(int iX){
              Foo::setX(iX);
              Foo::printX("Foo(int) Constructor");
              Foo::Foo();
              Foo::printX("Foo(int) Constructor");
  
          };  
  };
  
  int main( int argc, char** argv ){
  
      Foo bar(2);
  
      return 0;
  }
  </code></pre>
  
  <p>The output of which is</p>
  
  <pre><code>setX(void) Method :  Foo::iX = 2
  Foo(int) Constructor :  Foo::iX = 2
  Foo(void) Constructor :  Foo::iX = 1
  Foo(int) Constructor :  Foo::iX = 2
  </code></pre>
  
  <p>As the results indicate <code>setX</code> method works as expected. <code>Foo::iX</code> is equal to <code>2</code> inside and outside of scope of that function.</p>
  
  <p>However when calling the <code>Foo(void)</code> constructor from within the <code>Foo(int)</code> constructor, <code>Foo::iX</code> stays equal to <code>1</code> only within that constructor. As soon as it exits out that method, it reverts back to <code>2</code>.</p>
  
  <p>So my question is 2-fold:</p>
  
  <ol>
  <li>Why does C++ behave this (a constructor cannot be called from within another constructor without values that were assigned)?</li>
  <li>How can you create multiple constructor signatures, but without redundant duplicate code doing the same thing?</li>
  </ol>