What's going on in this code, anyway? It's a reference to a pointer, or a pointer to a reference, or something else?
– Stephen GrossDec 13 '11 at 19:22

1

Right to left reading, ptr is a reference to pointer of type char.
– CoderDec 13 '11 at 19:31

Just curious: Is there really a situation where this approach is warranted? It seems like there should be an more understandable way to solve the problem...?
– Stephen GrossDec 13 '11 at 19:54

Slowly C++'izing a C style code... You slowly migrate away from C issues like *p[i] = b; / **p = b; / etc. at the same time having the transition period to shared_ptr<char> etc. which is incompatible with the codebase without larger code changes.
– CoderDec 13 '11 at 20:00

1

@kevincline: It was a hypotetical problem :D What if you could return 2 if the pointer is the same, just reallocated, and so on. The question is still, if such parameter modifications are bad or good as they are quite invisible in the code.
– CoderDec 14 '11 at 19:47

3 Answers
3

The problem with your code sample isn't that you take a reference, it's that you use a return code. In C++, if the operation fails, throw an exception, or if that isn't unusual, then use boost::optional, or even boost::variant<T, ErrorCode>.

If you’re concerned about mutability, make references const unless they need to be mutable, and leave it at that. A function’s name and documentation should convey its purpose and whether it intends to modify one of its parameters.

Users of the function shouldn’t need to care whether something is passed by value or reference; the “invisibility” of references—that they have the same syntax as values—is one of their strengths. The reason for introducing references to C++ in the first place was to “invisibly” support operator overloading. In addition to their consistency, they are also amenable to generic programming, which is a big thing in the C++ world. And in C++0x, it gets more interesting: you can pass variables syntactically by value, but they can be move-constructed, eliminating the need for a copy.

Well, move construction is usually natural, the copy construction has issues, because copy constructors usually have side effects, like copying buffers, sharing shared states etc. And unexpected behavior for move constructor is not really unexpected, vs sudden change in values passed by reference.
– CoderDec 13 '11 at 19:54

Everything about your code example is C and not C++. I think you have bigger issues than whether you are passing a pointer by reference or not. For example, I would suggest using new and delete rather than malloc.

To answer you question directly, use references everywhere you can and pointers when you have to - ie when you need to reseat the reference. All of this is in the C++ FAQ which is a mine of brilliant information.