Re: better understanding references
From: thides (swatts_at_globalserve.net)
Date: 03/04/04
- Next message: Mike Wahler: "Re: [FAQ] free c/c++ compile for win"
- Previous message: David White: "Re: better understanding references"
- In reply to: David White: "Re: better understanding references"
- Next in thread: Mike Wahler: "Re: better understanding references"
- Reply: Mike Wahler: "Re: better understanding references"
- Reply: David White: "Re: better understanding references"
- Reply: Wolfie: "Re: better understanding references"
- Reply: Greg Comeau: "Re: better understanding references"
- Reply: jeffc: "Re: better understanding references"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 4 Mar 2004 00:27:47 -0500
"David White" <no@email.provided> wrote in message
news:_Fv1c.2731$KS1.102703@nasal.pacific.net.au...
>
> "thides" <swatts@globalserve.net> wrote in message
> news:1_u1c.67773$jQ4.10628@nntp-post.primus.ca...
> > Re: C++ FAQ Lite
> > Copyright 1991-2003
> > Marshall Cline
> >
> > 8.1 What is a reference?
> > <snip>
> >
> > i++;
> >
> > <snip>
> > "...A C programmer will think of this as if you used the C style
> > pass-by-pointer, with the syntactic variant of (1) moving the & from the
> > caller into the callee,..."
> >
> > I dont understand a 'variant of (1)' in particular unless it means that
1
> is
> > added to the value which is passed to by the underlying pointer or
moving
> > the & (reference) from the caller to the callee esp in the i++ example.
>
> It means that the & (address of) of the caller is moved to the function
> argument of the callee, where the & defines the argument as a reference.
I'm
> not sure I like this in the FAQ, because it's just an accident that the
same
> symbol is used for address-of and reference. Also, the & is not always
> needed to take an address.
When is & (address of ) not used to take an address?
>
> >
> > "and (2) eliminating the *s. "
> >
> > I dont see a *s.
>
> He's saying that *s is what the C programmer would use to access the value
> the pointer points to, which is true, and that it is eliminated when
> references are used instead.
Dereferencing to get the value -- gotchya.
I still dont know where this *s came from or what happened to its data type.
>
> > "In other words, a C programmer will think of i as a macro for (*p),
where
> p
> > is a pointer to x (e.g., the compiler automatically dereferences the
> > underlying pointer; i++ is changed to (*p)++; i = 7 is automatically
> changed
> > to *p = 7). "
> >
> > I do understand the idea of i as a abreviation for *p, where p is a
> pointer
> > to x and the example the compiler automatically dereferences the
> underlying
> > pointer the rest of the line is kind of hazy. Would someone please
expand
> on
> > this idea to make understanding easier. I kind of get it but its not
> > crystal.
>
> He's saying that if you think of the reference as a pointer, the compiler
> does the dereferencing (i.e., access of the value the pointer points to,
> like *p) for you. So, if you have i = 7, it is the equivalent of *p = 7 if
> you replace the reference with a pointer.
How can you replace the reference with a point in the swap function? I have
tried it all and any ways but the only way that will work is the one below.
>
> In my opinion, this explanation in the FAQ is a very bad one. It just
> reinforces, wrongly, the idea that references and pointers are somehow
> related. They aren't. A reference is an alias for an object. For example:
> int k = 3;
> int &i = k;
>
> Here, i is being used as an alias for k. That is, where you use i you are
> really using k.
>
> Now, take the code in the FAQ:
>
> void swap(int& i, int& j)
> {
> int tmp = i;
> i = j;
> j = tmp;
> }
>
> int main()
> {
> int x, y;
> ...
> swap(x,y);
> ...
> }
>
> The swap function takes two reference parameters, i and j. That means that
i
> and j will be aliases for the objects passed to the function. The objects
> being passed are x and y. That means that where i is used in the function,
> it really refers to x (not a _copy_ of x, but the _actual_ x), and j is
used
> as an alias for y. This is why passing by reference is fundamentally
> different from passing by value. Instead of the function receiving a copy
of
> the passed argument, it receives a reference to the actual object passed.
>
> Forget thinking of references in terms of pointers. It's wrong.
>
> DW
>
>
>
- Next message: Mike Wahler: "Re: [FAQ] free c/c++ compile for win"
- Previous message: David White: "Re: better understanding references"
- In reply to: David White: "Re: better understanding references"
- Next in thread: Mike Wahler: "Re: better understanding references"
- Reply: Mike Wahler: "Re: better understanding references"
- Reply: David White: "Re: better understanding references"
- Reply: Wolfie: "Re: better understanding references"
- Reply: Greg Comeau: "Re: better understanding references"
- Reply: jeffc: "Re: better understanding references"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|