title: Right way to conditionally initialize a C++ member variable?
body: <p>I'm sure this is a really simple question. The following code shows what I'm trying to do:</p>
  
  <pre><code>class MemberClass {
  public:
      MemberClass(int abc){ }
  };
  
  class MyClass {
  public:
      MemberClass m_class;
      MyClass(int xyz) {
          if(xyz == 42)
              m_class = MemberClass(12);
          else
              m_class = MemberClass(32);
      }
  };
  </code></pre>
  
  <p>This doesn't compile, because <code>m_class</code> is being created with an empty constructor (which doesn't exist). What is the right way of doing this? My guess is using pointers and instantiating <code>m_class</code> using <code>new</code>, but I'm hoping there is an easier way.</p>
  
  <p><strong>Edit:</strong> I should have said earlier, but my actual problem has an additional complication: I need to call a method before initializing m_class, in order to set up the environment. So: </p>
  
  <pre><code>class MyClass {
  public:
      MemberClass m_class;
      MyClass(int xyz) {
          do_something(); // this must happen before m_class is created
          if(xyz == 42)
              m_class = MemberClass(12);
          else
              m_class = MemberClass(32);
      }
  };
  </code></pre>
  
  <p>Is it possible to achieve this with fancy initialization list tricks?</p>