Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

27
Oct

When to use pointer, when to use reference?

Related Blog Items

Use references when you can, and pointers when you have to. References are usually preferred over pointers whenever you don’t need “reseating”. This usually means that references are most useful in a class’s public interface.

References 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 a “sentinel” reference. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references should always alias objects, not a dereferenced NULL pointer).

Note: Old line C programmers sometimes don’t like references since they provide reference semantics that isn’t explicit in the caller’s code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine

Popularity: 11%

You need to log on to convert this article into PDF


Related Blog Items

1 Comment

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image