Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if anybody can point to a post, pdf, or excerpt of a book containing the rules for C++ variable life-times and best practices for passing and returning function parameters. Things like when to pass by value and by reference, how to share ownership, avoid unnecessary copies, etc. This is not for a particular problem of mine, I've been programming in C++ for long enough to know the rules by instinct, but it is something that a lot of newcomers to the language stumble with, and I would be glad to point them to such a thing.

share|improve this question

closed as too broad by ChrisF Dec 2 '14 at 22:57

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

Stroustrups "The C++ Programming Language" maybe. Marshall P. Clines C++ FAQ – thorsten müller Nov 16 '12 at 10:08
Object destruction in C++ Has two great articles that describe exactly how lifetimes work in C++ – Loki Astari Nov 16 '12 at 16:45
up vote 4 down vote accepted

Here's the rules in a summary post:

  1. pass-by-value + copying is related + slicing
  2. pass-by-reference + polymorphism is related
  3. ownership passing is (usually) done via pointers.
  4. sizeof(T) is compile-time constant, making all c++ types have compile-time size
  5. ownership sharing via shared_ptr
  6. never pass ownership -style
  7. escaping from function local scope to upper scope requires copying the data, because the local scope data storage disappears after function call ends.
  8. call stack is one path in a tree; function call is moving to a subtree
  9. User-defined types are special - order of classes in header file is important for inheritance and composition - class scope lifetime can be tied to either heap or stack
  10. Polymorphic factory usually uses ownership passing; but can also be implemented using copying/clone and pass-by-reference.
  11. Avoiding unnecessary copies is bad practise - copying should be supported for all data.
  12. Interfaces only support passing compile-time amount of data through the interface. All data needs to be split to small pieces; and dynamic amount of it requires repeated function calls to the interface.
  13. Constructor parameters allow adding parameters to object
  14. All heap allocation in arrays is outside of the type system -- there does not exists type that can contain dynamic amount of memory inside it.
share|improve this answer
Hey thanks. I think that your first 5 points should be understandable to any newcomer. And for the others she will need to do some research, but that's ok. – dsign Nov 16 '12 at 15:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.