Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Trashcann

C++... Reference Parameters

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

http://www.truevision3d.com/html/modules.php?op=modload&name=phpBB2&file=viewtopic&t=3543 can someone please review that post and tell me what you think? thanks, Matt

Share this post


Link to post
Share on other sites
Advertisement
any second opinions on this?
i had someone tell me the opposite of what he said,
they said the first one was better because the second would risk NULL

Share this post


Link to post
Share on other sites
Either one will screw up if you pass in NULL. No two ways about that.

The general rule for references and pointers, which most good C++ programmers ascribe to, is to use references "whenever possible" and use pointers "wherever else". That means that almost all function arguments will be references; the only real place where you use pointers are where you have to reseat them.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The first one is definetly the way to go (use the reference and not the pointer).

References in C++ cannot be NULL. For more information, see "More Effective C++" by Scott Meyers, Item number 1 (page 10 specifically).

Many features of the C++ language can be defeated by underhandedness (like changing the private members of a class for example), but that shouldn''t prevent you or anyone else from using the language properly.

Share this post


Link to post
Share on other sites
To nitpick, there''s no risk of NULL reference or NULL pointer in the specific example, because that new expression can''t return NULL.

In general I''d say any function accepting a pointer argument should check for NULL while there''s no need to check for NULL reference arguments. Based on that the first one is better.

Passing a pointer to something makes it more obvious (in the calling code) that the argument will be modified; some people prefer that and avoid using non-const reference arguments, but that''s a style preference rather than a technical reason.

Share this post


Link to post
Share on other sites
quote:
Original post by spock
To nitpick, there''s no risk of NULL reference or NULL pointer in the specific example, because that new expression can''t return NULL.

In general I''d say any function accepting a pointer argument should check for NULL while there''s no need to check for NULL reference arguments. Based on that the first one is better.

Passing a pointer to something makes it more obvious (in the calling code) that the argument will be modified; some people prefer that and avoid using non-const reference arguments, but that''s a style preference rather than a technical reason.


NULL is returned on failure. Here''s the proof:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_25.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/conve_12.asp

Trashcan,

On looking at the code with ''fresh eyes'' what Sneftel says is perfectly correct.

You will need to check for NULL before using add2() to prevent undefined behaviour.

Share this post


Link to post
Share on other sites
quote:
Original post by Mathematix
NULL is returned on failure.

For a standard-conforming compiler, the required behavior is to return a non-null pointer or throw a bad_alloc exception. If you want a null pointer as an error indication, you need to use the nothrow version of new.

AFAIK Visual C++ .NET conforms to the standard in this respect while earlier versions did not.

[edited by - spock on October 7, 2003 6:28:45 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!