#### Archived

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

This topic is 5287 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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 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 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 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 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 on other sites
I always pass by reference. It is a lot better then always havind to de-reference the pointer.

##### 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.

The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

##### 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.

The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

1. 1
2. 2
Rutin
22
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631735
• Total Posts
3001940
×