title: C++ checking singleton pointers
body: <p>I have an application, which has a (Qt C++) singleton logger class. The GetInstance() implementation is:</p>
  
  <pre><code>if(m_Instance == NULL)
  {
      try
      {
          m_Instance = new Logger();
      }
      catch(...){}
  }
  return m_Instance;
  </code></pre>
  
  <p>Now I have following macro in the .h file:
  "#define LOG Logger::Instance()->Log"</p>
  
  <p>Everything is fine as long as new() operation works. What is the best way to ensure that pointer has set (I was thinking some try-catch blocks to catch the std::bad_alloc, but I don't know how to implement that in a macro)? I have created a workaround, which seems to work but is not pretty:</p>
  
  <p>"#define LOG if(Logger::Instance()) Logger::Instance()->Log"</p>
  
  <p>In addition, I would like to know what if my object has a lot getters/setters (e.g. setPath(), getSize()...)? Currently, I have a macro:</p>
  
  <p>"#define SET Settings::Instance()"</p>
  
  <p>and in code I'm able to use SET->setPath("abc"); or SET->getSize();</p>
  
  <p>In this case my ugly workaround does not work, because it requires separate macro for every method. Any tips how I can improve that?</p>
  
  <p>Thanks for the answers.</p>