title: C++ static member variable scope
body: <p>The title basically says it all, i wonder when static members of a c++ class are initialized and when they go out of scope.</p>
  
  <p>I need this for the following problem. I have many objects of a class Foo and each object needs access to a resource, encapsulated by another class Bar. Synchronization is not an issue, so i want all objects to share the same Bar instance.</p>
  
  <p>I'm using a simple managed pointer for reference counting.</p>
  
  <p>Can i do the following:</p>
  
  <pre><code>class Foo {
  private:
      static managed_pointer&lt;Bar&gt; staticBar;
  public:
      Foo() {
          if(!staticBar)
              staticBar = new Bar;
      }
      /*
       * use staticBar in various non-static member functions
       */
  };
  
  managed_pointer&lt;Bar&gt; Foo::staticBar = NULL;
  </code></pre>
  
  <p>the managed_pointer staticBar should delete the Bar object as soon as it goes out of scope - but when does this happen? when the last instance of Foo is destructed? on application exit?</p>
  
  <p>Thanks for your advice!</p>