Archived

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

pointers or references?

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

I''m new to this pointer and reference stuff. All I really want to ask is if the reference approach in the below function is a better way to proceed with then using pointers and pointing to NULL (better practise). Can you set a ref to NULL if you wanted to test against it as another option? BOOL AlphaBlending::enableAlphaBlending(Graphics *Graphics, BOOL Enable, DWORD Src, DWORD Dest) { if(Graphics->getDeviceCOM() == NULL) return FALSE; // Enable or disable if(FAILED(Graphics->getDeviceCOM()->SetRenderState(D3DRS_ALPHABLENDENABLE, Enable))) return FALSE; // Set blend type if(Enable == TRUE) { Graphics->getDeviceCOM()->SetRenderState(D3DRS_SRCBLEND, Src); Graphics->getDeviceCOM()->SetRenderState(D3DRS_DESTBLEND, Dest); } return TRUE; } Is this one below a better practise? BOOL AlphaBlending::enableAlphaBlending(Graphics &Graphics, BOOL Enable, DWORD Src, DWORD Dest) { // Enable or disable if(FAILED(Graphics.getDeviceCOM()->SetRenderState(D3DRS_ALPHABLENDENABLE, Enable))) return FALSE; // Set blend type if(Enable == TRUE) { if(FAILED(Graphics.getDeviceCOM()->SetRenderState(D3DRS_SRCBLEND, Src))) return FALSE; if(FAILED(Graphics.getDeviceCOM()->SetRenderState(D3DRS_DESTBLEND, Dest))); return FALSE; } return TRUE; }

Share this post


Link to post
Share on other sites
References cannot be set to anything representing NULL. Well, you can sort of do it, by making your object have a method such as bool IsNull(). But regardless, if you have a reference parameter, then you''re going to have to pass in an object.

I would say that references are good when you want to force the user of the function to pass in a valid object. Plus they simply look cleaner. Pointers would be better (easier) if you want to allow a NULL value, indicating that there is no object passed in.

Most of the time, though, choosing between references and pointers is a matter of personal preference. Some people like reading code with references, because it looks cleaner. Other people like code with pointers, because they can easily see that they indeed are passing in the address, not a copy, which is what references do anyway; it''s just that references make it look the same as when you pass in a copy.


int Agony() { return *((int*)0); }    Mwahaha... >8)

Share this post


Link to post
Share on other sites
This is just personal preference, but I tend to use pointers if there''s any chance that the function modifies its arguments. If not, I''ll pass a const reference. It compiles to pretty much the same code in the end; the only real difference is syntatic.

One difference: a reference can''t be NULL; it has to refer to a valid object. So, you could pass references if you want to make sure that you can''t receive a null pointer.

Share this post


Link to post
Share on other sites
References can''t be null and once created always refers to the same object.

If a parameter can be null you have to use a pointer, if the parameter isn''t modified use a const ref or value.
For parameters that are modified you can use either references or pointers. If you use references it is invisible to the caller that the object may be modified, if you use pointer it is visible at the call-site.

Share this post


Link to post
Share on other sites
quote:
Original post by Trap
References can''t be null


If you initialize reference with NULL pointer isn´t it pointing to NULL then?

Like this:


int *pointer = NULL;
int &reference = *pointer;

if (&reference == NULL)
// TRUE


Share this post


Link to post
Share on other sites
can you dereference a null-pointer?

edit: seems like you can. didn't know this was possible...


[edited by - Trap on March 26, 2004 6:19:11 PM]

Share this post


Link to post
Share on other sites
Dereferencing a null pointer has undefined behaviour. It could crash on the dereference or it could louse up perfectly good code later in the program. For a vaguely humorous look at the issue try reading this article.

Share this post


Link to post
Share on other sites
Trap: Yes I can.. Still I don´t say anything about consequences

But yeah, now I know.. That was just something that came to my mind.. Didn´t know that dereferencing is "illegal" in a situation like this..

Anyway, thanks SiCrane for enlightment.

Share this post


Link to post
Share on other sites
quote:
Original post by Mkk
quote:
Original post by Trap
References can''t be null


If you initialize reference with NULL pointer isn´t it pointing to NULL then?

Like this:


int *pointer = NULL;
int &reference = *pointer;

if (&reference == NULL)
// TRUE





Yes you could and you would be right. However the convention is that such code is evil and should never ever appear, and if it does that''s code error. In effect, you can safely assume that a reference always refers to a valid object.

Share this post


Link to post
Share on other sites