dereferencing a pointer when passing by reference

Dereferencing the pointer doesn’t create a copy; it creates an lvalue that refers to the pointer’s target. This can be bound to the lvalue reference argument, and so the function receives a reference to the object that the pointer points to, and returns a reference to the same. This behaviour is well-defined, and no temporary object is involved.

If it took the argument by value, then that would create a local copy, and returning a reference to that would be bad, giving undefined behaviour if it were accessed.

Leave a Comment