• Advertisement
Sign in to follow this  

References or Pointer's

This topic is 3384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Use references where possible, smart pointers where not, raw pointers when there is no other choice.

References cannot be null. References have the easiest syntax. References are not reseatable. References cannot be accidentally treated as arrays.

Prefer smart pointers to raw pointers where possible. It makes the intended ownership semantics explicit - while usually enforcing it implicitly. For example, boost::shared_ptr<Foo> means I am not the only person who accesses this Foo instance. In addition, boost::shared_ptr<> will automatically delete the instance when all sharing owners have been destroyed.

When all other alternatives are exhausted, you have raw pointers. You need to be careful about:

- manually managing resources (especially in the presence of exceptions).
- checking for NULL pointers.
- ensuring all pointers are correctly initialised
- unintended array access.
- accidental pointer arithmetic (e.g. p += 4 instead of *p += 4)
- clear ownership semantics.
- other miscellaneous pointer problems...

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement