Archived

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

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.

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 on other sites
The second one is better, because the first risks a NULL reference which is illegal.

hrm- ok
thanks ;D

Share on other sites
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 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.

ok- thanks

Share on other sites
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 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 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 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]

1. 1
2. 2
3. 3
Rutin
16
4. 4
JoeJ
13
5. 5

• 10
• 9
• 14
• 10
• 25
• Forum Statistics

• Total Topics
632646
• Total Posts
3007637
• Who's Online (See full list)

There are no registered users currently online

×