ProAnswers.org

When should I use references, and when should I use pointers in c++?

1 Like

References are usually preferred over ptrs whenever you don’t need reseating' This usually means that references are most useful in a class' public interface. References then typically appear on the skin of an object, and pointers on the inside. The exception to the above is where a function's parameter or return value needs asentinel’ reference. This is usually best done by returning/taking a pointer, and giving the nil ptr (0) this special significance (references should always alias objects, not a dereferenced nil ptr).