|
- Both references and pointers can be used to change local variables of one function inside another function. Both of them can also to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency again.
References are less powerful than pointers-
Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
-
References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
-
A reference must be initialized when declared. There is no such restriction with pointers.
References are safer and easier to use.-
Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don't refer to a valid location.
-
Easier to use: References don't need referencing operator to access the value. They can be used like normal variables. '&' operator is needed only at the time of declaration. Also, members of an object reference can be accessed with dot operator ('.'), unlike pointers where arrow operator (->) is needed to access members.
|