Re: better understanding references

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


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



Relevant Pages

  • Re: Question on LSP
    ... Sure, the developer must keep track of which type has been assigned to each pointer to manage complexity in creating the design, which is why the 'T' is in T*. ... Subtyping is a relation which does not ... Those object types are completely orthogonal to the semantics of being an object reference. ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Question on LSP
    ... Because construction paradigms have different goals. ... But that has nothing to do with what a pointer type ... But that does not mean that the semantics of an object reference is ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Question on LSP
    ... equivalent addresses in the same context. ... Note that the language allows us to use a name like 'T' on the reference as a mnemonic so that the developer can keep track of what is happening with the indirection. ... Modern languages have proper pointer types. ...
    (comp.object)
  • Re: Nothing Keyword Destories Objects rather than just resetting the variable to an empty variable
    ... there is no difference between using ByVal or ByRef. ... as the C1 pointer was the sole reference to the class object. ... Yes, an object variable is a pointer to an object, but that is the only ... the keyword "NOTHING" should only destroy the ...
    (microsoft.public.excel.programming)
  • Re: How java passes object references?
    ... to think of them as being a specific location in a larger block of memory ... Whether a language passes by reference or by value, ... is a pointer pointing at the memory block. ... So when allocating local memory for o, it would simply allocate a ...
    (comp.lang.java.programmer)