Effective C++ Ed. 3rd: Item 21. Don't try to return a reference when you must return an object

  1. Remember not to return reference to a local object/variable in any function. As local variable/objects dies as soon as function ends.
  2. Remember not to return a dynamically allocated object (using new operator). User of the function may forget corresponding delete call.
  3. Remember not to return reference to a local static object/variable in any function say f().
    In this case f(obj1, ...) = = f(obj2, ...) will always comes true.
  4. Instead return the object by value.
prev | next
Cheers and try hosting at Linode.

No comments:

Post a Comment