Jump to content
  • Advertisement
Sign in to follow this  

[C++] const reference or pointer to class.

This topic is 3125 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 a reference where possible, and const where possible. Try to avoid raw pointers in general. Use "const" if the function only calls methods marked as "const" on the object. Use a reference if the parameter may not be null, and is not an array. See here for more information on "const". For information on references vs pointers, read this. That entire site is worth bookmarking, its very good.

You can avoid using pointers by using something like boost::optional<> for... optional arguments and by using containers instead of passing arrays around. You should wrap pointers in smart pointer objects such as boost::shared_ptr<>, boost::weak_ptr<>, boost::scoped_ptr<> or std::auto_ptr<>, whichever applies. You might find that your compiler has shared_ptr<> available in the namespace "std::tr1".

One important difference is you can pass a temporary to a function expecting a const reference, but not to one that takes a non-const reference or any kind of pointer.

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!