Sign in to follow this  

References or Pointer's

This topic is 3314 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

This topic is 3314 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this