Jump to content
  • Advertisement

Archived

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

amish1234

pointers instead of reference: bad programming?

This topic is 5287 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''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)

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!