title: What's a good way of declaring instance variables in C++ without constructing them?
body: <p>I've been searching around the internet a lot for this topic, and I haven't really reached a firm answer. As mainly a C# programmer, I'm used to declaring classes in a large scope, typically near the top of the file, outside any functions, and then constructing them when in use.</p>
  
  <p>After moving on to C++, the only way of replicating this is to have a default constructor, which is fine, but in some cases, I'd rather have a constructor which requires arguments than an argument-less default constructor.</p>
  
  <p>After searching around the internet for a solution, I've come across a few pieces of advice, which have their flaws:</p>
  
  <p><strong>1. Pointers</strong></p>
  
  <p>Some people have suggested to have a dynamic pointer in the desired scope, and then assign the pointer to point at the location of the class when constructed.</p>
  
  <pre><code>CClass* pClass = 0;
  
  int main()
  {
      pClass = new CClass(1337);
  
      delete pClass;
      return 0;
  }
  </code></pre>
  
  <p>The problem with this approach is that you have to remember to delete the pointer afterwards, thus, static pointers are much more "safe". Also, I'm guessing there will be slight memory overhead doing this, albeit not much, due to having the pointer.</p>
  
  <p><strong>2. Have a default construct anyway</strong></p>
  
  <p>It is sometimes recommended to have a default constructor anyway, which just zeros everything inside the class:</p>
  
  <pre><code>class CClass
  {
  public:
      CClass() : leetNumber(0) {}
      CClass(int leetNumber) : leetNumber(leetNumber) {}
  private:
      int leetNumber;
  };
  
  //Defaults leetNumber to 0 through default ctor
  CClass myClass;
  
  int main()
  {
      myClass = CClass(1337);
  
      return 0;
  }
  </code></pre>
  
  <p>But what happens if you can't just zero everything inside the class? What if you have another class in there which cannot just be initialized to nothing? What will you do if the user tries to access a function inside the class without proper initialization of the members? (You could check for this, but I believe it would take too much code, especially if you have a lot of members).</p>
  
  <p><strong>3. Staying in a smaller, more local scope</strong></p>
  
  <p>There have been suggestions where people have said to stay in a small scope, pass the class as a reference to other functions that may need it, and construct as soon as they declare the class:</p>
  
  <pre><code>class CClass
  {
  public:
      CClass(int leetNumber) : leetNumber(leetNumber) {}
      int getLeetNumber() { return leetNumber; }
  private:
      int leetNumber;
  };
  
  bool GetMuchNeededAmazingNumberFromClass(CClass&amp; myClass)
  {
      if(myClass.getLeetNumber() == 1337)
          return true;
  
      return false;
  }
  
  int main()
  {
      CClass myClass = CClass(1337);
      if(!GetMuchNeededAmazingNumberFromClass(&amp;myClass);
          return 1;
  
      return 0;
  }
  </code></pre>
  
  <p>This is good in the sense that you can see what function needs what, but I can imagine a function needing a lot of external classes having a massive amount of needed arguments.</p>
  
  <p>There are many more examples, but I cannot seem to find one that I can rely on, especially coming from a C# background, where this stuff is nice and easy.</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Let me elaborate more on what I'm asking for - in C#, you can do the following:</p>
  
  <pre><code>public class Program
  {
      //See how I'm able to do this, without calling the ctor.
      static AmazingClass amazing;
  
      public static void Main()
      {
          //And then call the constructor when I want.
          amazing = new AmazingClass(1337);
      }
  }
  </code></pre>
  
  <p>This allows me to create the class without actually constructing it, this is what I'm looking for in C++.</p>
  
  <p>Thanks again.</p>