It's not returning the memory address, It's actually returning the object by non-const reference. The same way it was passed in. This might seem a bit of overkill because the calling code can either rely on the third parameter passed, which will be populated on return from the function, or the return parameter.
The reason for doing it this way is to allow chaining. So you can do:
split(myString, ',', asAVector).size().
which will perform the function and allow you to chain the results by calling a function on the vector (in this case size
)
Despite the neatness, there are some potential drawbacks to this approach: For example, no error code is present in the return value so you are reliant on the function either working proerly or throwing an exception; therefore you'd usually expect to wrap the above with try
/ catch
semantics. Of course, the more chaining you do, the more likely it will be that the possibilities for different types of exception will go up so you may have to cover more catch
blocks.
Mind you, passing back by reference is a whole lot better than passing back by pointer. Chaining with pointers is notorious for crashing when one of the functions in the chain decides to fail and return 0.