Re: better understanding references
From: thides (swatts_at_globalserve.net)
Date: 03/04/04
- Next message: Greg Comeau: "Re: Optimizing a switch statement"
- Previous message: Andrew Koenig: "Re: Optimizing a switch statement"
- In reply to: Gary: "Re: better understanding references"
- Next in thread: David White: "Re: better understanding references"
- Reply: David White: "Re: better understanding references"
- Reply: Mike Wahler: "Re: better understanding references"
- Reply: Old Wolf: "Re: better understanding references"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 3 Mar 2004 23:33:26 -0500
"Gary" <glabowitz@comcast.net> wrote in message
news:f6-dnecc3btOGdvdRVn-tw@comcast.com...
> "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>
> <snip>
> > 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.
>
> Stop trying to think about pointers, addresses and the rest. Think
instead:
>
> When I write a function with a parameter list containing &x, I am renaming
> the value being passed to me as "x." The identifier x is a name for a
> location in memory containing the value being passed.
>
> Caller... z = foo(y);
>
> Called... int foo(int &x) { ...
>
> The variable addressed by "y" is the same variable addressed by "x" even
> though the identifiers are in different code blocks.
>
> If you understand this, you understand references in general.
>
> int a;
> int &b = a;
>
> The two identifiers "a" and "b" each refer to the value in a single
> variable. The variable addressed by "a" is the same variable addressed by
> "b."
I guess if a changes then so will b. And if b changes so will a.
Like this:
int a;
int &b=a;
b=4; // therefore a=4
But that wont work because b is a reference to a value not a value itself...
OK I think if I am correct on this point i might be able to move on.
> --
> Gary
>
>
- Next message: Greg Comeau: "Re: Optimizing a switch statement"
- Previous message: Andrew Koenig: "Re: Optimizing a switch statement"
- In reply to: Gary: "Re: better understanding references"
- Next in thread: David White: "Re: better understanding references"
- Reply: David White: "Re: better understanding references"
- Reply: Mike Wahler: "Re: better understanding references"
- Reply: Old Wolf: "Re: better understanding references"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|