title: Declare object without calling default constructor
body: <p>I'm new to C++, and am used to working with Java.
  In Java, I have the option of declaring an object without instantiating it, and would like to do the same in C++.</p>
  
  <p>Assuming there is some class <code>Foo</code>, in Java I could write <code>Foo bar;</code> to declare an instance of <code>Foo</code> without initializing <code>bar</code>.
  However, in C++ when I write <code>Foo bar;</code>, <code>bar</code> is initialized by calling the default constructor of class <code>Foo</code>.</p>
  
  <p>This is particularly vexing if I have written one or more constructors for class <code>Foo</code>, each of which have at least one argument. In this case, the code will fail to compile with an error similar to <code>no matching function for call to 'Foo::Foo()'</code></p>
  
  <p>For example, say I have the following class definition in Java:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Foo {
      private boolean b;
  
      Foo(boolean b) { this.b = b; }
  }
  </code></pre>
  
  <p>and the corresponding class definition in C++:</p>
  
  <pre><code>class Foo {
          bool b_;
      public:
          Foo(bool b) : b_(b) {}
  };
  </code></pre>
  
  <p>In Java, I could write some method</p>
  
  <pre class="lang-java prettyprint-override"><code>public static Foo makeFoo(int x) {
      Foo result;
      if (x &gt; 10) { result = new Foo(true); }
      else { result = new Foo(false); }
      return result;
  }
  </code></pre>
  
  <p>However, if I write a similar method in C++, I get a compilation error:</p>
  
  <pre><code>Foo makeFoo(int x) {
      Foo result; // here, a call is made to Foo::Foo() which doesn't exist, and thus compilation fails
      if (x &gt; 10) { 
          result = Foo(true); // this would probably also fail since I didn't specify a copy-constructor, but I'm not entirely sure
      }
      else {
          result = Foo(false); // as above, I think this would probably fail
      }
      return result;
  }
  </code></pre>
  
  <p>While the example I gave is useless, I frequently used this sort of approach when writing Java code.
  Is there a way to emulate this behavior in C++?
  Alternatively, is this just bad design? If so, what sort of approach would you recommend?</p>