If we follow your reasoning all the way, nothing is ever passed by reference, since a reference is itself an object (it just happens to always point to the same thing). |
By my reasoning, which I infer from section 7.2 of Stroustrup, if a copy of the object is made and passed to the receiving function (and thus the receiving function can do whatever it likes to the object and, upon the return to the calling scope, the original object that was copied is unchanged), it's pass by value.
If messing around with the object (not dereferencing it and messing around with a
different object) in the function changes the object in the calling scope, it's pass by reference.
Would you really talk about that function by saying you pass the parameter by value? The immediate assumption would be that a copy of bar is made and bar can't be directly accessed from within foo(). |
I would say that is pass-by-value. If someone were to assume that a copy of bar is made, they've misread the code.
If we follow your reasoning all the way, nothing is ever passed by reference, since a reference is itself an object (it just happens to always point to the same thing). |
No, that is not what my reasoning says. My reasoning is that if a copy is made, and changes to that copy do not affect the original in the calling scope, it is pass by value. When you pass by reference, the receiver does not have a copy to play with; changing the passed object changes the original in the calling scope.
As an aside, I can think of occasions when passing a null or an uninitialised pointer would not be any kind of error. In fact, the circumstances you thought of that would justify sending in a null pointer apply equally to sending in an uninitialised pointer!
As another example, where an uninitialised pointer would be an error, I have some code that will test the provided pointer, and if it's null,
not carry out an operation (the pointer gets set to an output stream by another class, and I like these classes to take care of themselves, so they do their own testing on provided objects; I could do the testing before calling the class function, but as I said, in this case I like the objects to take care of themselves as much as possible).