&

Started by
14 comments, last by Tazel 21 years, 3 months ago
I thought the reason you used references over pointers was that they save you from explicitly dereferencing. You can pass them just as if you are passing by value and within a function you can treat them as if they had been passed by value. I try to only pass pointers if I intend to modify the pointer itself. The const keyword should save you from passing something which you do not want modified.
"C combines all the power of assembly language with all the ease of use of assembly language"
Advertisement
quote:Original post by CWizard
Out of curiosity, in what way are references more safe than pointers? Unless you do nasty casts (which you can do with references as well), which you shouldn''t, I can''t see what''s safer with references.


I just find I''m less likely to end up passing a NULL pointer, or an invalid pointer to a function if I use references.

Sure, you could dereference an invalid pointer, and pass it as a reference, but I find I''m a lot less likely to do that.

Of course if a function doesn''t want a NULL pointer, I can always use an assert to catch it, but why bother when I can use references? What if I forget to put the assert in?

Here''s an example of the kind of thing I''ve done when I''ve not been quite on the ball.


  CObject* object; //forgot to initialise pointerDoSomethingWithObject( object ); //BANG  


Now sure, if you''re careful that won''t happen, but I find it''s easy to make a mistake when you''ve been programming for hours.

I don''t see any ambiguity in a function call that uses references, but that''s probably because my first experience of C/C++, was with C++. We were taught to use references before we even learned what a pointer was. I''m used to references, I didn''t even use C until I did Playstation programming in my 4th year of uni. (Codewarrior bad)
quote:Original post by Oxyacetylene
CObject* object; //forgot to initialise pointer
DoSomethingWithObject( object ); //BANG

How does references save you in this case? I usually don't use reference in this way, but you would need to:
void DoSomethingWithObject(CObject &object); CObject *object;// You need to do thisDoSomethingWithObject(*object);// As this would be to pass a reference to CObject pointer (ie. type error)DoSomethingWithObject(object); 

And same error would occur as with a wild pointer, or...?

In either case, the compiler throws a warning in your face, so it isn't easy to miss.

EDIT: If object wasn't a pointer, but the object itself, there's no risk of it not being initialized.

Btw, I sometimes use references when passing certain types of objects, but never with basic types.



[edited by - CWizard on January 18, 2003 2:13:46 PM]
quote:CObject *object;
// You need to do this
DoSomethingWithObject(*object);

You''re the one dereferencing the invalid pointer and you can choose not to, therefore it''s safer. It puts the responsibility in your hands. However, what you''ll find is you can pretty much do away with pointers if you use references everywhere. As I said, you only really need pointers for pure C apis. So all this talk about checking will only occur at the api interface. Once you''ve established that the pointer is non-null you can proceed without any conditional stuff. If the pointer is null you can replace it with a null object, as I''ve described and again benefit from simpler code.

Pete
quote:Original post by petewood
You''re the one dereferencing the invalid pointer and you can choose not to, therefore it''s safer. It puts the responsibility in your hands.
I can''t make any sense of that. The context was the claim that references could save you from unitialized pointers. How am I supposed to pass an object to a function that expects a reference without dereferencing the pointer to the object?
quote:However, what you''ll find is you can pretty much do away with pointers if you use references everywhere.
Yes, in most cases, but the question is why? A reference is a pointer, why obscure that fact?
quote:As I said, you only really need pointers for pure C apis.
Huh? How do you replace pointer arithmetic with references?

quote:How am I supposed to pass an object to a function that expects a reference without dereferencing the pointer to the object?

Exactly, you have to ensure that the object you pass is valid.

If passing a null pointer really makes sense then pass a nullObject instead.
quote: Huh? How do you replace pointer arithmetic with references?

Pointer arithmetic? Do you mean for arrays? I use vectors and iterators or whatever container is appropriate. Say bye-bye to pointers!

[edited by - petewood on January 19, 2003 1:41:34 AM]

This topic is closed to new replies.

Advertisement