Q:

When should I use references, and when should I use pointers?

0

When should I use references, and when should I use pointers?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

In a single statement, “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 a reference that does not refer to an object. 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).

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions For Experienced

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How virtual functions are implemented in C++?... >>
<< How to call a non-const member function from a con...