int& value vs. int* pValue???

Started by
11 comments, last by Spartacus 22 years, 9 months ago
Can someone tell me what the point in defining a function like this is:

int fnA(int& value)
{
    value = 10;
}
 
instead of doing it like this:

int fnB(int* pValue)
{
    *value = 10;
}
 
As far as I can see they both do the same thing. -René

Real programmers don't document, if it was hard to write it should be hard to understand

Advertisement
I only use references when I want to pass in-only structures. Passing structures by value is inefficient and passing them by pointer if you''re not going to be changing them is annoying, so you pass them by const reference instead.

e.g. void func(const HugeStruct &hs);

Then I can call it as func(hs) instead of func(&hs) and not lose efficiency.

Passing something by a non-const reference (like you''re doing with fnA) is generally bad because it''s not obvious from the code that fnA will change the value or not. e.g. If I say fnA(x) I''d be really annoyed if x came back different.

-Mike
-Mike
Just a note, you could get a nasty exception if you pass NULL
to that second function. Consider that. They are similar but
not really the equal.
The exception can get even nastier if it is an uninitialized pointer instead of NULL. But I think that would be one of the main advantages of references. You don''t have to test for NULL in your functions and still be able to feel safe about it

You can also access structure data members as if it was a local data structure. i.e.: myStruct.member as opposed to myStruct->member


Seeya
Krippy

On the other hand, pointer input arguments can act as a tri-state (in digital design-speak), so they can have benefits too. Example:
    void Widget::setColor (COLORREF *fg, COLORREF *bg){  if (fg)    m_clrFg = *fg;  if (bg)    m_clrBg = *bg;}    


Obviously, if I want to just set the foreground or just the background and leave the other the way it was, I can do so with the above code. You can't do that with references.

I like to use pointers where I'd like the option of not specifying a parameter, and references when the parameter must exist. However, I have heard that you should never use references in the parameter list because it confuses those who haven't quite left their C roots behind. I don't agree, but I have heard it; if you're working in a team environment, you might consider the style of your coworkers before deciding to go reference-happy.


Edited by - Stoffel on June 29, 2001 12:11:59 PM
Pathological example to defy the logic of the last few post:
Why & is not any more-or-less exception proof than *.
  int A;int* p=0;fnA(A); //no problem, not nullfnB(&A); /no problem, it can''t be null either.fnB(p); //BOOM!fnA(*p); //Whoops, also BOOM!  


The only difference is what you type; they even work the same way beneath the code.
With the exception of operator overloads, you should only pass by const&, a-mon-avis. If you''re going to change it, clue them in with a *.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
actually you can use both ways
it''s just for when one reads the code he will understand it better!

hmm now another thing is that
when you use a & you HAAAAAVVVVVEEEEE to refere some object

and when you use * you can point a null
but your function will have to check for it before using it

in references you MUST refer to something
otherwise......CRAP


Arkon
[QSoft Systems]
int A;
int* p=0;
fnA(A); //no problem, not null
fnB(&A); //no problem, it can''t be null either.
fnB(p); //BOOM!
fnA(*p); //Whoops, also BOOM!

Well that last line is not gonna boom inside of fnA, so it''s not really the same. It is going to boom when trying to come up with an answer for *p to push onto the stack before calling fnA. *p means ''what is at the address of p'' which the system will never let you know since p is null.

Seeya
Krippy
Don''t forget the all-important reason, the one why references were added to C++: return references for function chaining.

For those that use it anyway. It''s how

cout << "Hello " << "world!";

works. The << operator for cout returns a reference to itself. So after each call you''re left with ''cout'' again. Imagine this using pointers:

*(cout << "Hello ") << "world!";

All this to replace printf-style formatting. Go figure.
Ahhhhhhhhhhh!

I was mistaken.

It does not actually try to resolve *p before calling fnA. So you would need to test if (&value != NULL) in Magmais example.

Oops

Seeya
Krippy

This topic is closed to new replies.

Advertisement