C++... Reference Parameters

Started by
15 comments, last by Trashcann 20 years, 6 months ago
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
ermokwww.truevision3d.com
Advertisement
The second one is better, because the first risks a NULL reference which is illegal.
hrm- ok
thanks ;D
ermokwww.truevision3d.com
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
ermokwww.truevision3d.com
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.
ok- thanks
ermokwww.truevision3d.com
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.

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.
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.

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]

This topic is closed to new replies.

Advertisement