Daniel T. said:
OK, so add that to the formulation. That's all I'm saying here, the
quote you provided doesn't tell the whole story because you can use
references even where passing by value makes more sense.
Your example of passing &i is what caught my attention. I just
had to clean up a bunch of code written by a former employee.
He decided to write functions that took a std::vector*. When he
called these functions, if he had a std::vector, he passed it
by &vec. He didn't like iterators.
If it isn't our function, then we aren't going to be making any
decisions about what the parameter types are. The discussion assumes we
are the one's making the decision, so of course it's our function.
Reseating a pointer within a function has no effect on the calling
function either. Your previous example, as I pointed out, was
pointless.
Well you can dereference a pointer and do pretty much
what you want with it.
When designing a function, we can make the parameter type T, const T&,
T&, const T*, or T*. The OP asked how to decide between the five
options. I'm interested in your answer. How do you decide between them?
Can your formulate your decision process as an algorithm?
Not really as an algorithm. It would be nice if you could
create algorithms to tell you how to design software
on this level but it would be hard to cover all the possibilities.
Sometimes you need to use pointers. For
example, if you use Borland's VCL, all of their TObject stuff must
be pointers. With Qt, it's often the case though not exclusively.
When you write functions, you don't always get to choose the
types that you're working with.
But given generic code, assuming that I don't want to modify
the variable, if it's no more efficient to use a reference, I would
just pass it by value.
If the object is expensive to copy like a vector<spoo>, I would
use const &.
Beyond that, it depends on the circumstances.