title: simple singleton example in c++
body: <p>I am trying to implement singleton that I have used before in PHP and Java 8, to C++. But I do face certain restrictions from the syntax and how C++ works (specifically pointers).</p>
  
  <p>This is what I have tried so far:</p>
  
  <pre><code>#include "iostream"
  
  using namespace std;
  
  class System{
  protected:
      static System *obj;
  
  public:
      static System *getInstance(){
          return obj;
      }
  
      void prn(){
          cout&lt;&lt;"this works!";
      }
  };
  
  int main(void){
      System &amp;sys = System::getInstance();
      sys.prn();
  }
  </code></pre>
  
  <p>while executing, I get the following error:</p>
  
  <pre><code> sameer.cpp:20:10: error: non-const lvalue reference to type 'System'
   cannot bind
         to a temporary of type 'System *'
           System &amp;sys = System::getInstance();
                   ^     ~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  
  <p>Please help me solve this error.. as I have no idea what it means. I have checked the forum before posting, and it can be a possible duplicate of previously asked question (which I caould not find).. But I posted this because I wanted to understand the meaning of error my code generated.</p>
  
  <p>Thanks for the help</p>