Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Don't do this:

Code Block
  std::string& f();
  const std::string& g(); // Not much better

Instead do this:

Code Block
  std::string f();
  std::string g();

std::string is designed expressly to allow you to treat strings as simple pass-by-value types, like int. It's efficient to return by value rather than reference and it avoids core dumps if the real string hidden away in f gets deleted before the reference. In particular it allows f() to compute once-off values and forget about them, e.g.:

...