title: Static variable for object count in c++ classes?
body: <p>I would like to have a static member variable keep track of the amount of objects that have been made. Like so:</p>
  
  <pre><code>class test{
      static int count = 0;
      public:
          test(){
              count++;
  
          }
  }
  </code></pre>
  
  <p>That doesn't work because, according to VC++, <code>a member with an in-class initializer must be constant</code>. So I looked around and apparently you're supposed to do:</p>
  
  <pre><code>test::count = 0;
  </code></pre>
  
  <p>Which would be great, but I want count to be private.</p>
  
  <p><strong>edit:</strong>
  Oh boy, I just realized I need to do:</p>
  
  <pre><code>int test::count = 0;
  </code></pre>
  
  <p>I had seen something just do <code>test::count = 0</code>, so I assumed you wouldn't have to declare type again.</p>
  
  <p>I'd like to know if there's a way to do this inside the class though.</p>
  
  <p><strong>edit2:</strong></p>
  
  <p>What I'm using:</p>
  
  <pre><code>class test{
      private:
          static int count;
      public:
          int getCount(){
              return count;
          }
          test(){
              count++;
  
          }
  }
  int test::count=0;
  </code></pre>
  
  <p>Now it's saying: <code>'test' followed by 'int' is illegal (did you forget a ';'?)</code></p>
  
  <p><strong>edit3:</strong></p>
  
  <p>Yup, forgot a semicolon after the class definition. I'm not used to having to do that. </p>