search
Login

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

0 votes
84 views
When should I use references, and when should I use pointers?
asked Geek 15.7k points 5 9 21

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 Advisor 5.6k points 1 7 14

Related questions

1 vote
1 answer 25 views
1 vote
2 answers 4.2K views
1 vote
1 answer 2.3K views
1 vote
1 answer 102 views
0 votes
1 answer 422 views
422 views
which loop is better in C and C++, for loop or while loop or do while?
asked anonymous
...