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

3,749 questions

4,049 answers

114 comments

10,705 users

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

0 votes
80 views
When should I use references, and when should I use pointers?
asked May 28, 2011 by albertina Geek (15,670 points)

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 May 28, 2011 by daniel Advisor (5,600 points)

Related questions

+1 vote
1 answer 25 views
+1 vote
2 answers 4,230 views
4,230 views asked Jun 10, 2010 by anonymous
+1 vote
1 answer 2,310 views
+1 vote
1 answer 99 views
99 views asked May 23, 2010 by anonymous
0 votes
1 answer 417 views
...