title: Is there a way to late-initialize a member variable (a class) in C++?
body: <p>I am coming from the Java background. I have the following program. </p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  
  class First {
      public:
      First(int someVal): a(someVal) {
  
      }
      int a;
  };
  
  class Second {
      public:
      First first;
      Second()  {   // The other option would be to add default value as ": first(0)"
          first = First(123);
  
      }
  };
  
  int main()
  {
      Second second;
      std::cout &lt;&lt; "hello" &lt;&lt; second.first.a &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>In class <code>Second</code>, I wanted to variable <code>first</code> to remain uninitialized until I specifically initialize it in <code>Second()'s constructor</code>. Is there a way to do it? Or am I just left with 2 options?:</p>
  
  <ol>
  <li>Provide a parameter-less constructor. </li>
  <li>Initialize it with some default value and later re-assign the required value.</li>
  </ol>
  
  <p>I can't initialize <code>first</code> in the initializer-list with the right value, since the value is obtained after some operation. So, the actual required value for <code>first</code> is available in <code>Second()</code> constructor only. </p>