title: How to best handle C++ object initialization: empty constructors or pointers?
body: <p><strong>I'm wondering what is the best way to go about object initialization and storage</strong> with regards to objects that have to have a relatively large scope / long lifetime. Let's say we have a <code>GameEngine</code> class that needs to initialize and hold a reference to a <code>Window</code> for rendering. The reference is needed throughout the program's lifetime and the window needs to know its dimensions, at least.</p>
  
  <p>In Java, I'd do it like this:</p>
  
  <pre><code>// Declaration:
  Window window;
  // Initialization:
  window = new Window(width, height);
  </code></pre>
  
  <p>I understood that in C++, the first would already call the default constructor of the Window class, hence be <strong>declaration</strong> and <strong>initialization</strong>. Having a <code>window = Window(width, height);</code> would therefore be <strong>assignment</strong>, throwing away the already existing object.</p>
  
  <p><strong>The first solution I could find was to use a pointer</strong>:</p>
  
  <pre><code>// GameEngine.hpp
  class GameEngine {
      Window *window;
  };
  
  // Somewhere in GameEngine.cpp:
  window = new Window(width, height);
  </code></pre>
  
  <p>But then again, I constantly read one should favor plain objects over pointers whenever possible and in fact, I got myself into a mess of pointers in no time, so I am looking for another way.</p>
  
  <p><strong>Another solution seems to design your objects to have a constructor without parameters and set up the object later on</strong>:</p>
  
  <pre><code>// GameEngine.hpp
  class GameEngine {
      Window window;
  };
  
  // Somewhere in GameEngine.cpp
  window.setWidth(width);
  window.setHeight(height);
  </code></pre>
  
  <p>This works, but has a serious drawback: the object (at least in this case) could be in an inconsistent state, as trying to display the window without setting width/height would result in an error or crash. It does work for some objects, but for most it does not.</p>
  
  <p><strong>One way to avoid this would be to have default values. For example, the constructor for the Window class could look like this</strong>:</p>
  
  <pre><code>Window::Window(int width = 800, int height = 600) {}
  </code></pre>
  
  <p><strong>Or even like that</strong>:</p>
  
  <pre><code>Window::Window() : width(DEFAULT_WIDTH), height(DEFAULT_HEIGHT) {}
  </code></pre>
  
  <p>But in many cases, default values will be hard to determine. Also, where should they be coming from? Should the Window class define <code>DEFAULT_WIDTH</code> and <code>DEFAULT_HEIGHT</code>? <strong>Or should I even do this?</strong></p>
  
  <pre><code>// GameEngine.hpp
  class GameEngine {
      static const int DEFAULT_WIDTH = 800;
      static const int DEFAULT_HEIGHT = 600;
      Window window(800,600);
  };
  </code></pre>
  
  <p>But that seems bad, as I've read that you should not do any initialization in the header, only declaration, so the values of <code>DEFAULT_WIDTH</code> and <code>DEFAULT_HEIGHT</code> should not actually be known at this point (and only be initialized in the .cpp, correct?).</p>
  
  <p><strong>Am I missing an option?</strong> Or is it common in C++ to assume that the programmer should know what he's doing and take care of getting his objects in a consistent state before using them? <strong>When to use which approach?</strong></p>