title: What is the life time of a singleton class's member variables?
body: <pre><code>class Member {
  public:
      Member(int idx) {
          this-&gt;idx_ = idx;
      }
  
      int get_idx() {
          return idx_;
      }
  
      void set_idx(int idx) {
          this-&gt;idx_ = idx;
      }
  
      void foo();
  
  private:
      int idx_;
  };
  
  
  class SingletonClass {
  public:
      static SingletonClass&amp; GetInstance() {
          static SingletonClass inst;
          return inst;
      }
  
      vector&lt;Member*&gt; get_members() {
          return this-&gt;members_;
      }
  
      Member* get_member(int idx) {
          return this-&gt;members_[idx];
      }
  
      void add_member(Member* mem) {
          this-&gt;members_.push_back(mem);
      }
  
  private:
      SingletonClass() {}
      vector&lt;Member*&gt; members_;
  };
  
  void Member::foo() {
      SingletonClass inst = SingletonClass::GetInstance();
      cout &lt;&lt; inst.get_members().size();
  }
  
  
  int main() {  
      SingletonClass inst = SingletonClass::GetInstance();
      inst.add_member(new Member(0));
      inst.add_member(new Member(1));
      cout &lt;&lt; inst.get_members().size() &lt;&lt; endl;
  
      Member* mem = inst.get_member(1);
      mem-&gt;foo();
      return 0;
  }
  </code></pre>
  
  <p>The output is</p>
  
  <pre><code>2
  0
  </code></pre>
  
  <p>It seems that when I get the instance of SingletonClass inside <code>Member::foo()</code>, the member variable vector <code>members_</code> of <code>SingletonClass</code> is cleared... Can anyone explain to me why and how this happen? And how can I fix it to get expected result? Thanks a lot!</p>