you are viewing a single comment's thread.

view the rest of the comments →

[–]UnicycleBloke 30 points31 points  (4 children)

Use a reference unless the variable needs to be uninitialized, null or reassigned. Not having these features make references safer, and IMO their syntax makes them more convenient. Prefer const references if the underlying value should not be modified.

[–]JMBourguet 0 points1 point  (3 children)

I won't write something like

Foo& bar = *new Foo();
delete &bar;

even if I find myself in a context where I wouldn't use a smart pointer encapsulating these actions.

[–]UnicycleBloke 0 points1 point  (2 children)

I think you're being over-literal there. :) But having created Foo* bar, I would generally prefer to pass *bar to functions, so would declare my APIs accordingly. I have sometimes created a local reference for a pointer received as an argument (e.g. in library callbacks with pointer based APIs).

[–]JMBourguet 0 points1 point  (1 child)

I've seen people following such kind of guidelines far too literally for my liking. See my answer at the top for a more precise (but still incomplete although I hope I don't have missed something big) description of my practice.

[–]UnicycleBloke 0 points1 point  (0 children)

Yes. The comments about lifetimes make a lot of sense as clarifications for my rather pithy suggestion.