title: Efficient ways to initialize class members; both heap and stack allocated
body: <p>Assume we have the following:</p>
  
  <pre><code>//! SomeClass.hpp
  class 
  {
  public:
      SomeClass( void );
  
     ~SomeClass( void ) 
      { 
         delete mFoo; 
         delete mBar; 
      }
      ...
  
  private:
      Foo* mFoo;
      Bar* mBar;
      StackObj mStackFoo;
  };
  
  //! SomeClass.cpp
  SomeClass::SomeClass( void )
  {
       mFoo = new Foo;
       mBar = new Bar; 
       mStackFoo = StackObj( ... );
  }
  </code></pre>
  
  <p>Now, when we initialize the pointers, my understanding is that the constructor will <a href="http://www.parashift.com/c++-faq/init-lists.html" rel="nofollow">create unnecessary copies</a> of SomeClass' members, thus allocating <strong>and then deallocating</strong> memory simply for the sake of allocating memory.</p>
  
  <hr>
  
  <p>It's common to use initializer lists, coupled with a separate initialization function (for heap allocated memory) as a method to avoid this. Say <code>SomeClass</code> has a private member function defined as <code>void initHeapMem( void )</code>. Then we can do,</p>
  
  <pre><code>SomeClass::SomeClass( void )
      : mFoo( NULL ),
        mBar( NULL ),
        mStackFoo( ... )
  {
       initHeapMem();
  }
  
  void SomeClass::initHeapMem( void )
  {
      mFoo = new Foo;
      mBar = new Bar;
  }
  </code></pre>
  
  <p>Naturally, this <em>somewhat</em> fixes the problem. The issue here I believe is that there's still the overhead of another function call being performed. </p>
  
  <p>The reason why we can't use initialization lists for raw pointers is because they're <strong>not thread safe</strong>. If something goes wrong, and the program throws an exception, there will still be a memory leak. <strong>Note: this is according to what I've read, my apologies if this is wrong</strong>.</p>
  
  <hr>
  
  <p>So, with boost/C++11, we can use smart pointers from an <code>#include &lt;tr1/memory&gt;</code> directive in the header file ( assuming we're using STL ).</p>
  
  <p>If we were to use, say, <code>std::unique_ptr&lt; T &gt;</code>, then we'd have <code>Bar* mBar</code> and <code>Foo* mFoo</code> replaced with:</p>
  
  <pre><code>std::unique_ptr&lt; Foo &gt; mFoo;
  std::unique_ptr&lt; Bar &gt; mBar;
  </code></pre>
  
  <p>Which would then allow us to do,</p>
  
  <pre><code>SomeClass::SomeClass( void )
     mFoo( new Foo ),
     mBar( new Bar ),
     mStackFoo( ... )
  {
  }
  </code></pre>
  
  <p>Since the smart pointer effectively <em>wraps</em> the memory allocation in <em>its own</em> constructor.</p>
  
  <p>While this is a nice solution, I personally am not one to use smart pointers for every heap object I create, and I know there are others in the C++ community who feel the same way.</p>
  
  <hr>
  
  <h2><strong>tl;dr</strong></h2>
  
  <p>With all of that out of the way, what I'm really wondering is <strong>if there are any more efficient alternatives to initializing class members within an object</strong> (especially with the advent of C++11), apart from the ones I listed above.</p>