Re: better understanding references

From: Greg Comeau (comeau_at_panix.com)
Date: 03/04/04


Date: 4 Mar 2004 08:34:19 -0500

In article <IBy1c.2736$KS1.102886@nasal.pacific.net.au>,
David White <no@email.provided> wrote:
>"thides" <swatts@globalserve.net> wrote in message
>news:67y1c.68104$9j1.25598@nntp-post.primus.ca...
>> 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
>
>Correct.
>
>> But that wont work because b is a reference to a value not a value
>itself...
>
>What do you mean "that won't work"?

I suspect that thides is thinking that:

int a = 99;
int &b = a;

since often a use of a means 99, that the reference is referring
to that and not a, after all, we don't use &a. However, b is
indeed referring to a and not the 99 per se.

>b is another name for a, or b _refers_ to a (hence it is a reference). If
>you assign b to 4,

assign 4 to b

>a and b will equal 4,

Something like that.

>because a and b refer to the same value.

Refer to the same object.

>If you assign a to 4, a and b will again equal 4.

4 to a.

-- 
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?


Relevant Pages

  • Re: pointer/ref question
    ... >> passing by reference? ... In the call to f, I'm passing x by reference, but ... >used which is probably not technically correct and could be misinterpreted, ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Book Suggestions
    ... >Reference as a reference where needed. ... >traps when only using it as a reference to the standard library functions. ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Book Suggestions
    ... >Reference as a reference where needed. ... >traps when only using it as a reference to the standard library functions. ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.cpp)
  • Re: classes in functions
    ... >> reference it or is it already referenced,,, for changing the values in the ... this is not a pointer: ... Books, good books, always help. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: GCC question
    ... int is the default and where is the difference between no parameter ... type declaration whereas the new one is much to confusing. ... the prototype (without missing info) tends to cause it to be upfront. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.c)