Re: better understanding references

From: thides (swatts_at_globalserve.net)
Date: 03/04/04


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



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... int create(int id, int scale, unsigned char *image); ... unsigned char* ptrImage = NULL; ... // Read through entire pointer byte by byte and put into managed array ...
    (microsoft.public.dotnet.framework.interop)
  • Re: type names
    ... reference operator to make your overloading works. ... I would have presumed, in place of references, one just passes pointers. ... and unless there is an intention to do some pretty heavy lifting, it will continue being a pointer. ... FOO operator+(FOO x, int a) ... ...
    (comp.std.c)