title: Whats the right approach to return error codes in C++
body: <p>I'm using error codes for handling errors in my c++ project. The problem is how to return error codes from a function which is supposed to return some variable/object. </p>
  
  <p>consider this:</p>
  
  <pre><code>long val = myobject.doSomething();
  </code></pre>
  
  <p>Here, myobject is an object of some class. If doSomething function encounters some error condition then how should it notify the caller (Without using exceptions).</p>
  
  <p>Possible solutions:</p>
  
  <ol>
  <li>Have a data member (say err_) in the class which can be checked by the caller. But it would be unsafe in a multi-threaded application sharing the same object and calling the same function.</li>
  <li>Use some global error variable, again same issue in a multi-threaded environment.</li>
  </ol>
  
  <p>Now how can I notify the caller about some error condition?</p>