title: C++ initialization of local/global object
body: <p>I've already encountered many time the case when there's a class, e.g. <code>LogFile</code>, that has no default constructor, while I want to initialize an instance of it, not on the heap.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>LogFile logfile;
  
  void init(const std::string&amp; path) {
      logfile = LogFile(path);
  }
  </code></pre>
  
  <p>The above code will not compile, since <code>LogFile</code> class has no default constructor.</p>
  
  <p>There are a few workarounds I currently use:</p>
  
  <p>1.
  Persisting not to make any heap calls manually, storing the object in a list (the list will for sure use the heap, but I won't care about it)</p>
  
  <pre><code>std::list&lt;LogFile&gt; logfile_holder;
  
  void init(const std::string&amp; path) {
      logfile_holder.push_back(LogFile(path));
  }
  </code></pre>
  
  <p>2.
  Using the heap, with a shared pointer/unique pointer:</p>
  
  <pre><code>std::shared_ptr&lt;LogFile&gt; logfile_ptr;
  
  void init(const std::string&amp; path) {
      logfile_ptr.reset(new LogFile(path));
  }
  </code></pre>
  
  <p>I there a more standard solution for doing such things?</p>
  
  <p>Even something that looks like the list workaround, but with a template class that is designated specifically for this purpose, without the overhead of the list, would be nice.</p>