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

3,749 questions

4,049 answers


10,705 users

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

0 votes
When should I use references, and when should I use pointers?
asked 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 by daniel Advisor (5,600 points)

Related questions

+1 vote
1 answer 25 views
+1 vote
2 answers 4,235 views
+1 vote
1 answer 2,315 views
+1 vote
1 answer 101 views