pointers instead of reference: bad programming?

Started by
17 comments, last by amish1234 20 years, 2 months ago
I''ve often seen code like this:


void Function(int* a)
{
   *a=5;
}

....
void main()
{
  int myint;
  Function(&myint);
}
The reason Function''s parameter is of type int* is so that the variable you pass to it will be equal to 5 after the function is called. But isn''t this what a reference is for? It should be like this:


void Function(int &a)
{
   a=5;
}

....
void main()
{
  int myint;
  Function(myint);
}
The first version might lead other people to think that I want you to pass an int*, when really I just want the int you pass to be equal to 5 even after the function call. Is there any reason I would want to do this with a pointer instead of a reference? If not, is this "bad" programming? ___________________________________________________________ Proceeding on a brutal rampage is the obvious choice. Where to find the intensity (Updated Jan 23, 2004)
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
Advertisement
the first method you showed was the C way. they did not have reference variables. so you had to pass everything as a pointer.

the other way is the C++ way and they had reference variables

ie. int number;
int &number1 = number;

in any case, they are both correct and one is no more wrong than the other.

Beginner in Game Development?  Read here. And read here.

 

I still prefer pointers... I find references leave things a little ambiguous. Passing a const pointer (not a pointer to a const) achieves the same thing and you aren''t left with that confusion.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
Passing as a reference is safer. There's also the const-ref. The only time I use pointers directly in parameters now is if I have an array.

edit: not only that, the syntax is cleaner too.

[edited by - Odoacer on January 25, 2004 8:30:46 PM]
I''ve just made the transition from C to C++ myself, and I''m still a bit confused about when I should use references vs. pointers. I prefer not having to throw a & on what I''m passing, but if that variable changes in the function, I like the &. (I find & is a good warning... be careful, this function might change that variable). Ask me in another 6 months.
I know some (quite a few actually) people who prefer to use a pointer if you are going to modify the variable, because it forces you to take the address of the variable, so it looks different when you call the function. For instance:

// This would tell them that the function does not modify
// the variable.
SomeFunction(var);

// This would tell them that the function DOES modify
// the variable.
SomeFunction(&var);

It makes sense. If you''re trying to debug something, and you just see: SomeFunction(var), and you used a reference, then you may forget that SomeFunction() modifies var. It''s not a big deal though.

I use a reference unless there is some reason to use a pointer, such as if I''m doing some pointer arithmetic, or bitwise operations on pointers
quote:Original post by Russell
// This would tell them that the function does not modify
// the variable.
SomeFunction(var);

// This would tell them that the function DOES modify
// the variable.
SomeFunction(&var);


This is almost as bad as using Hungarian Notation for naming
variables.

One of the main argument against HN is that modern IDE provides
mouse-over tool-tip showing the type of a variable.

The same functionality works to show the parameters of
a function (just move the mouse cursor over the name of
a function in VS.NET, for example).


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
I always pass by reference. It is a lot better then always havind to de-reference the pointer.
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
I try to avoid writing functions that manipulate parameters like that.

But if I''m in a jam I go for the pointer since it''s much more obvious what''s going to happen. I try to never pass a non-const reference, unless its a return value maybe.

Image loads when I''m online since I''m too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

My stuff.Shameless promotion: FreePop: The GPL god-sim.
quote:Original post by tangentz
One of the main argument against HN is that modern IDE provides
mouse-over tool-tip showing the type of a variable.


That''s true, but the REAL problem with HN is that it produces ugly nausiating source code for no real benefit. The coder should have a good idea what type signature a function ought to be. The problem is that if the programmer isn''t sure what type a function accepts and gets it wrong it will not compile (unless the types can be converted, but that should be a trivial case), but if the programmer doesn''t realise that a function he uses modifies an argument given it will compile, and could produce buggy code. This is why I think this sort of pointer notation, which is much cleaner than HN garbage, is more important than HN and is appropriate in this case.

Image loads when I''m online since I''m too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

My stuff.Shameless promotion: FreePop: The GPL god-sim.

This topic is closed to new replies.

Advertisement