Re: pointer/ref question
From: Leor Zolman (leor_at_bdsoft.com)
Date: 02/25/04
- Next message: Leor Zolman: "Re: When to introduce exception safety"
- Previous message: Tom Junior: "Re: Pointer Arithmetic"
- In reply to: Vicki: "pointer/ref question"
- Next in thread: Gavin Deane: "Re: pointer/ref question"
- Reply: Gavin Deane: "Re: pointer/ref question"
- Reply: Paul: "Re: pointer/ref question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 25 Feb 2004 13:46:02 GMT
On 24 Feb 2004 22:15:02 -0800, wft2vicki@earthlink.net (Vicki) wrote:
>Hi,
>
> Can someone help me with the following code? I don't understand why
>the & (reference...is it called specifier?) is needed with the
>pointer.
>
>function header:
>
>void insertAfter(List Cell*& p, int k, int x);
>
>I thought that with the pointer, it is already getting passed by
>reference
You've already gotten good responses that illustrate the basic picture.
Part of the problem here, IMO, is terminology, and it would seem that
there's quite a bit of confusion over the meaning of the term "pass by
reference". Those that have been arguing the point (no pun intended) for
years seem to have settled on an interpretation of that term that they try
to disassociate with anything to do with "pointers" in C or C++, but I
think that is a losing battle in practical terms. I use the term "pass by
reference" to generically indicate we're conveying the address of an
object. Thus, to /me/ one can describe both of these situations as "passing
x by reference" in C or C++, while the second would only qualify as such
in C++ (as there's no reference type in C). I'm not adverse to also using
the term "pass by pointer" for the first case when teaching C++, in order
to have a separate term for that and passing to a function taking a
parameter by "real" reference:
void f(int *v) {...};
void g(int &v) {...};
int main()
{
int x;
f(&x); // C and C++
g(x); // C++ only
}
The key is always use the term "by reference" in context: /What/ are you
passing by reference? In the call to f, I'm passing x by reference, but I'm
passing a pointer to x by value. I'd be doing exactly the same thing if
I'd created a pointer variable initialized to &x and passed /that/ by
value.
I've gotten somewhat flamed for using the terms this way in the past, and
it'll probably happen again in the future (perhaps Real Soon Now), but at
least my point of view represents a framework where it is possible to
describe most, if not all, kinds of argument passing in C and C++ with a
minimal number of ambiguous terms...
-leor
>, so why the needed & operator. Isn't this like sending the
>address twice?
>
>What am I missing?
>Vicki
Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
- Next message: Leor Zolman: "Re: When to introduce exception safety"
- Previous message: Tom Junior: "Re: Pointer Arithmetic"
- In reply to: Vicki: "pointer/ref question"
- Next in thread: Gavin Deane: "Re: pointer/ref question"
- Reply: Gavin Deane: "Re: pointer/ref question"
- Reply: Paul: "Re: pointer/ref question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|