title: Static local variable gets reinitialized
body: <p>I have a virtual singleton that i want to implement the <code>getInstance()</code> static function for, however each time it is called, the static object gets re-initialised, so i get a new instance each time, any help will be appreciated - this has me very confused.</p>
  
  <p>The class I want to implement the method in:</p>
  
  <pre><code>class Pc : public Machine
  {
    private:
      ... members ...
  
    public:
      static Pc* getInstance();
  
      Pc() {};
      virtual ~Pc() {}
  
      ... other functions ...
  };
  </code></pre>
  
  <p>The parent class:</p>
  
  <pre><code>class Machine
  {
    public:
      static Machine* getInstance();
  
      Machine() { }
      Machine(const Machine&amp;) = delete;
      virtual ~Machine() { }
  
      ... methods ...
  
      void operator=(const Machine&amp;) = delete;
  };
  </code></pre>
  
  <p>Bridge from Machine -> Pc singleton</p>
  
  <pre><code>Machine* Machine::getInstance()
  {
      return Pc::getInstance();
  }
  </code></pre>
  
  <p>I have two sets of code for PC, one which i belive should work, and my current workaround code...</p>
  
  <p>Non working code:</p>
  
  <pre><code>Pc* Pc::getInstance()
  {
      static Pc* pc = new Pc();
      return pc;
  }
  </code></pre>
  
  <p>Working (but bodged) <code>getInstance()</code> code:</p>
  
  <pre><code>static Pc* pc = nullptr;
  Pc* Pc::getInstance()
  {
      if(pc == nullptr) {
          pc = new Pc();
      }
      return pc;
  }
  </code></pre>
  
  <p>While both compile successfully, after break pointing my code i can see that my expected code returns the same pointer, however after manipulating the object the second call returns a new object, leading me to believe the static variable as been initialized again.</p>
  
  <p>Compiled with the flags:</p>
  
  <pre><code>-ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -std=gnu++11 -lgcc
  </code></pre>
  
  <p>(this is for an OS project)</p>