Developer community
0 votes
85 views
When should I use references, and when should I use pointers?
asked by Geek (15.7k points) 5 9 21 | 85 views

1 Answer

0 votes
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).
answered by Advisor (5.6k points) 1 7 14

Related questions

+1 vote
1 answer
+1 vote
2 answers
+1 vote
1 answer
+1 vote
1 answer
asked by anonymous | 106 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users