equality of references

Discussion in 'C++' started by pauldepstein@att.net, May 9, 2009.

  1. Guest

    Suppose

    const int& i = 7;
    const int& j = 7;


    Is i==j true? My compiler says yes. On the other hand, as I
    understand it, equality of references means that the addresses being
    referenced are identical. Hence, as I understand it, it is quite
    possible that the integer 7 is being represented at two different
    addresses, and that two different addresses contain a representation
    of 7 and so i==j may be false. Is this correct?
    I'm not clear on the issue of equality of references.

    Thanks for your enlightenment.

    Paul Epstein
     
    , May 9, 2009
    #1
    1. Advertising

  2. On May 9, 11:17 pm, wrote:
    > Suppose
    >


    > const int& i = 7;
    > const int& j = 7;
    >
    > Is  i==j   true?


    I'd say that as a reference is sometimes considered as "another name"
    for a variable.
    Comparing 2 references is just comparing those(the referenced ones)
    variables, and 2 ints with the same value will always be equal.

    Arkaitz
     
    Arkaitz Jimenez, May 10, 2009
    #2
    1. Advertising

  3. On May 10, 12:17 am, wrote:
    > Suppose
    >
    > const int& i = 7;
    > const int& j = 7;
    >
    > Is  i==j   true?  My compiler says yes.  On the other hand, as I
    > understand it, equality of references means that the addresses being
    > referenced are identical.  Hence, as I understand it, it is quite
    > possible that the integer 7 is being represented at two different
    > addresses, and that two different addresses contain a representation
    > of 7 and so i==j may be false.  Is this correct?
    > I'm not clear on the issue of equality of references.
    >
    > Thanks for your enlightenment.


    If you want reference equality than do this:

    (&i) == (&j)

    This will tell you if i and j are really at the same memory location.
    -Andrew.
     
    Andrew Tomazos, May 10, 2009
    #3
  4. James Kanze Guest

    On May 10, 12:17 am, wrote:
    > Suppose


    > const int& i = 7;
    > const int& j = 7;


    > Is i==j true?


    Is 7 == 7? Obviously.

    > My compiler says yes. On the other hand, as I understand it,
    > equality of references means that the addresses being
    > referenced are identical.


    Where did you get that. A reference is just an alias for an
    object. Once it is initialized, it behaves exactly as if it
    were the aliased object.

    > Hence, as I understand it, it is quite possible that the
    > integer 7 is being represented at two different addresses, and
    > that two different addresses contain a representation of 7 and
    > so i==j may be false. Is this correct?


    No.

    > I'm not clear on the issue of equality of references.


    It sounds like you're not clear on the issue of references in
    general. Within an expression (any expression), references, as
    such, don't exist---a reference is just an lvalue expression
    referring to the object used to initialize it.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 10, 2009
    #4
  5. Guest

    On May 10, 12:05 pm, James Kanze <> wrote:
    > On May 10, 12:17 am, wrote:
    >
    > > Suppose
    > > const int& i = 7;
    > > const int& j = 7;
    > > Is  i==j   true?

    >
    > Is 7 == 7?  Obviously.
    >
    > > My compiler says yes.  On the other hand, as I understand it,
    > > equality of references means that the addresses being
    > > referenced are identical.

    >
    > Where did you get that.  A reference is just an alias for an
    > object.  Once it is initialized, it behaves exactly as if it
    > were the aliased object.
    >
    > > Hence, as I understand it, it is quite possible that the
    > > integer 7 is being represented at two different addresses, and
    > > that two different addresses contain a representation of 7 and
    > > so i==j may be false.  Is this correct?

    >
    > No.
    >
    > > I'm not clear on the issue of equality of references.

    >
    > It sounds like you're not clear on the issue of references in
    > general.  Within an expression (any expression), references, as
    > such, don't exist---a reference is just an lvalue expression
    > referring to the object used to initialize it.
    >
    > --
    > James Kanze (GABI Software)             email:
    > Conseils en informatique orientée objet/
    >                    Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    I understand more about references than it appears. Suppose int& f
    (int, int) is a function that returns a reference to a static local
    variable. Then f(x,y) == f(a,b) is always true for all integers
    x,y,a,b . I'm not clear exactly why although I've seen several
    attempts to explain it.

    Paul Epstein

    Paul Epstein
     
    , May 10, 2009
    #5
  6. Neelesh Guest

    On May 10, 9:23 pm, wrote:
    > On May 10, 12:05 pm, James Kanze <> wrote:
    >
    > I understand more about references than it appears.  Suppose int& f
    > (int, int) is a function that returns a reference to a static local
    > variable.  Then f(x,y) == f(a,b) is always true for all integers
    > x,y,a,b .  I'm not clear exactly why although I've seen several
    > attempts to explain it.
    >


    Lets see if a small analogy can help. Suppose you have a small sheet
    of paper on which you write, say the number 3. Lets imagine two cases
    (a) Return by value - This means that you do a photocopy of this sheet
    and give the photocopy to your friend. Then you take the original
    sheet, and change 3 written on it to, say 33. Then you see the sheet
    given to your friend to check if that sheet also got 33 on it. The
    answer is - obviously not. This is because what you returned to your
    friend is a _copy_, not the original sheet.
    (b) Return by reference - This means that you gave the original sheet
    to your friend. And then, you change 3 written on the original sheet
    to 33. Now you check whether the sheet given to your friend has 3 or
    33. Since both of the sheets are actually the same, the sheet given to
    your friend also has 33.

    In the same way, suppose f(a,b) returns local static variable by
    referece and the return value was, say 3. Suppose f(x,y) changes this
    3 to say 33. Since what f(a,b) returns is _same_ as (and not the copy
    of) the local static varible, when f(x,y) changes to 33, the return
    value of f(a,b) also gets changed to 33. As a result, f(a,b) == f(x,y)

    Please note that the above analogy is just in order to explain the
    current situation.

    Hope this helps.
     
    Neelesh, May 10, 2009
    #6
  7. SG Guest

    On 10 Mai, 18:23, wrote:
    > I understand more about references than it appears.  Suppose int& f
    > (int, int) is a function that returns a reference to a static local
    > variable.  Then f(x,y) == f(a,b) is always true for all integers
    > x,y,a,b .


    No. This is not true for this function:

    int& f(int a, int b) {
    static int p = 3;
    static int q = 4;
    if (((p^q) & 1)==0) return p;
    return q;
    }

    int main() {
    assert( f(0,0) == f(0,1) ); // fails
    }

    > I'm not clear exactly why although I've seen several
    > attempts to explain it.


    People tried to explain because you don't seem to understand
    references. Once a reference has been initialized the reference will
    be just another name for the object it refers to. So, if you use the
    equality comparison you'll be comparing the values of the aliased
    objects and not their addresses. Here are a bunch of other examples:

    int main() {
    int i = 23;
    int j = 23;
    int& x = i; // x is an alias for i
    int& y = j; // y is an alias for j
    assert( x == y); // #1
    assert(&x != &y); // #2
    assert(&x == &i); // #3
    assert(&y == &j); // #4
    x = 42;
    assert( x != y); // #5
    assert(&x != &y); // #2
    assert(&x == &i); // #3
    assert(&y == &j); // #4
    }

    1. x and y refer to variables of the same value (23)
    2. x and y refer to different variables
    3. x refers to i
    4. y refers to j
    5. x and y refer to variables of different values (23,42)

    Cheers!
    SG
     
    SG, May 10, 2009
    #7
  8. Guest

    On May 10, 7:50 pm, SG <> wrote:
    > On 10 Mai, 18:23, wrote:
    >
    > > I understand more about references than it appears.  Suppose int& f
    > > (int, int) is a function that returns a reference to a static local
    > > variable.  Then f(x,y) == f(a,b) is always true for all integers
    > > x,y,a,b .

    >
    > No. This is not true for this function:
    >
    >   int& f(int a, int b) {
    >     static int p = 3;
    >     static int q = 4;
    >     if (((p^q) & 1)==0) return p;
    >     return q;
    >   }
    >
    >   int main() {
    >     assert( f(0,0) == f(0,1) ); // fails
    >   }
    >
    > > I'm not clear exactly why although I've seen several
    > > attempts to explain it.

    >
    > People tried to explain because you don't seem to understand
    > references. Once a reference has been initialized the reference will
    > be just another name for the object it refers to. So, if you use the
    > equality comparison you'll be comparing the values of the aliased
    > objects and not their addresses. Here are a bunch of other examples:
    >
    >   int main() {
    >     int i = 23;
    >     int j = 23;
    >     int& x = i; // x is an alias for i
    >     int& y = j; // y is an alias for j
    >     assert( x ==  y); // #1
    >     assert(&x != &y); // #2
    >     assert(&x == &i); // #3
    >     assert(&y == &j); // #4
    >     x = 42;
    >     assert( x !=  y); // #5
    >     assert(&x != &y); // #2
    >     assert(&x == &i); // #3
    >     assert(&y == &j); // #4
    >   }
    >
    > 1. x and y refer to variables of the same value (23)
    > 2. x and y refer to different variables
    > 3. x refers to i
    > 4. y refers to j
    > 5. x and y refer to variables of different values (23,42)
    >
    > Cheers!
    > SG


    Ok. I understand the issues now. Thanks for all who helped on this
    thread.
    There was a small misunderstanding. When I said

    "I'm not clear exactly why although I've seen several
    attempts to explain it."

    What I actually meant was this: "I'm not sure why f(x,y) == f(a,b) in
    the reference-to-static case, though from my reading and from browsing
    the web, I've seen several attempted explanations of the reference-to-
    static scenario."

    In particular, the "several attempts" were not by those on this
    newsgroup.

    Anyway, all is clear now, and thanks again.

    Paul Epstein
     
    , May 10, 2009
    #8
  9. James Kanze Guest

    On May 10, 6:23 pm, wrote:
    > On May 10, 12:05 pm, James Kanze <> wrote:
    > > On May 10, 12:17 am, wrote:


    [...]
    > > > I'm not clear on the issue of equality of references.


    > > It sounds like you're not clear on the issue of references
    > > in general. Within an expression (any expression),
    > > references, as such, don't exist---a reference is just an
    > > lvalue expression referring to the object used to initialize
    > > it.


    > I understand more about references than it appears. Suppose
    > int& f (int, int) is a function that returns a reference to a
    > static local variable. Then f(x,y) == f(a,b) is always true
    > for all integers x,y,a,b.


    No it's not.

    > I'm not clear exactly why although I've seen several attempts
    > to explain it.


    Try it with the following:

    int& f( int, int )
    {
    static int result = 0 ;
    ++ result ;
    return result ;
    }

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 10, 2009
    #9
  10. James Kanze Guest

    On May 10, 9:57 pm, James Kanze <> wrote:
    > On May 10, 6:23 pm, wrote:


    > > On May 10, 12:05 pm, James Kanze <> wrote:
    > > > On May 10, 12:17 am, wrote:


    > [...]


    > > > > I'm not clear on the issue of equality of references.
    > > > It sounds like you're not clear on the issue of references
    > > > in general. Within an expression (any expression),
    > > > references, as such, don't exist---a reference is just an
    > > > lvalue expression referring to the object used to
    > > > initialize it.

    > > I understand more about references than it appears. Suppose
    > > int& f (int, int) is a function that returns a reference to
    > > a static local variable. Then f(x,y) == f(a,b) is always
    > > true for all integers x,y,a,b.


    > No it's not.


    > > I'm not clear exactly why although I've seen several attempts
    > > to explain it.


    > Try it with the following:


    > int& f( int, int )
    > {
    > static int result = 0 ;
    > ++ result ;
    > return result ;
    > }


    Sorry for the above; it's wrong. (I was in the middle of doing
    something else when I answered it, and didn't really read the
    example correctly. My code is really a good example for why
    they are always equal.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 10, 2009
    #10
  11. On May 10, 7:50 pm, SG <> wrote:
    > No. This is not true for this function:
    >
    >   int& f(int a, int b) {
    >     static int p = 3;
    >     static int q = 4;
    >     if (((p^q) & 1)==0) return p;
    >     return q;
    >   }
    >
    >   int main() {
    >     assert( f(0,0) == f(0,1) ); // fails
    >   }


    Bug...

    - if (((p^q) & 1)==0) return p;
    + if (((a^b) & 1)==0) return p;

    -Andrew.
     
    Andrew Tomazos, May 11, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    474
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    485
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    642
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    760
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    911
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page