Pointer vs Reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Summercool

    #1

    Pointer vs Reference


    Can we confirm the following? also someone said, Java also has
    "reference" like in C++, which is an "implicit pointer":

    Pointer and Reference
    ---------------------

    I am starting to see what pointer and reference are and how they
    relate
    to each other.

    in the C era, a pointer *is* a reference. that's why when we have

    int a = 10;
    int *pi = &a;

    and you can "dereferenc e it":

    *pi = 20;

    Until when C++ comes along, then we have a new "reference" :

    int a = 10;
    int i =& a; // or int i = &a; i am not sure about the syntax.
    i = 20; // now both a and i are 20

    so this type of reference is an implicit pointer... it points to a,
    but
    you don't use the way in C (int *pi = &a) And when you use (i = 20),
    it does the dereference silently. (*pi = 20;)

    so a reference is new: a pointer but "looks like not a pointer".

    come to think about it, in Java and Ruby, they are like that too.

    a = Car.new

    a doesn't look like a pointer, but it is actually a pointer.

    we don't dereference it to get to the attributes like (*a).value = 10
    or a->value = 10 but just use a.value = 10

    So from this point on, a reference and a pointer are not the same... a
    reference is a pointer "that doesn't look like a pointer."

    they both points to something. but the syntax (or grammar) of usage
    doesn't look like it is a pointer in the C era. a reference is an
    "automatica lly dereferenced" pointer, shall we say? or an "implicit"
    pointer, or "silent" pointer.

    in PHP5,

    $a = 10;
    $b =& $a; # now $b implicitly points to $a
    $b = 20; # now $b implicitly points to $a, which is 20

    $a = new Foo("hello"); # $a implicitly points to a Foo object
    # the Foo object is 100 bytes,
    # but $a is just 4 bytes

    $b =& $a; # $b implicitly points to $a.
    # $b is a pointer to pointer
    # $b points to a four byte pointer, which is $a

    $b = new Foo("ok"); # dereference $b and sets its content to
    # a new pointer to another object Foo("ok")
    # that is, $a points to Foo("ok") now
    # $b still points to $a, which points to
    Foo("ok")

    So now, when you print $b and $a, they are both Foo("ok")


    In language we use nowadays, which language has "reference"

    In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.

    In language we use nowadays, which language has "reference" to a
    "reference" ?

    I think C++, Java, PHP5 do... not sure about Python and Ruby.

  • Joshua Cranmer

    #2
    Re: Pointer vs Reference

    Summercool wrote:
    Can we confirm the following? also someone said, Java also has
    "reference" like in C++, which is an "implicit pointer":
    >
    Pointer and Reference
    ---------------------
    >
    I am starting to see what pointer and reference are and how they
    relate
    to each other.
    >
    in the C era, a pointer *is* a reference. that's why when we have
    >
    int a = 10;
    int *pi = &a;
    >
    and you can "dereferenc e it":
    >
    *pi = 20;
    >
    Until when C++ comes along, then we have a new "reference" :
    >
    int a = 10;
    int i =& a; // or int i = &a; i am not sure about the syntax.
    int i = &a; is the syntax. Yes, the ampersand is used for both reference
    and address-of, so it can be confusing (especially since I think that i
    would be set to the address of a in C).
    i = 20; // now both a and i are 20
    >
    so this type of reference is an implicit pointer... it points to a,
    I wouldn't call it an implicit pointer: you cannot, to my knowledge,
    change i to point to any other object but a like you can with a pointer.
    Rather, i is an alias of a: the two objects forever more point to the
    same thing.

    so a reference is new: a pointer but "looks like not a pointer".
    Not quite. See above.
    come to think about it, in Java and Ruby, they are like that too.
    No, it is not. In syntax, Java more nearly follows the reference syntax
    but it is closer to a pointer with transparent {de}referencing .
    In language we use nowadays, which language has "reference"
    >
    In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.
    >
    In language we use nowadays, which language has "reference" to a
    "reference" ?
    >
    I think C++, Java, PHP5 do... not sure about Python and Ruby.
    >
    Let me start by asking a question to the C++ language lawyers out there:
    what should this print out:

    int a = 50, b = 20;
    int i = &a;
    std::cout << "a: " << a << " b: " << b << " i: " << i;
    i = 30;
    std::cout << "a: " << a << " b: " << b << " i: " << i;
    i = &b;
    std::cout << "a: " << a << " b: " << b << " i: " << i;
    b = &a;
    std::cout << "a: " << a << " b: " << b << " i: " << i;
    a = &i; // Creating a circular loop?
    std::cout << "a: " << a << " b: " << b << " i: " << i;

    Java does not have double referencing or referencing as I understand the
    C++ spec (I, unfortunately, no longer have my draft copy of the spec).
    Every non-primitive type is closer to a pointer in JVM implementations
    (double-pointers, actually, under most implementations ). I think PHP5
    tends to follow the C++ way. I don't know anything about Ruby and I
    think that Python follows the Java style of what's going on.


    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth

    Comment

    • Rolf Magnus

      #3
      Re: Pointer vs Reference

      Summercool wrote:
      >
      Can we confirm the following? also someone said, Java also has
      "reference" like in C++, which is an "implicit pointer":
      Java references are neither like C++ references nor like C++ pointers. They
      are something in between.
      in the C era, a pointer *is* a reference.
      Depending on how you define "reference" , you could say that.
      Until when C++ comes along, then we have a new "reference" :
      >
      int a = 10;
      int i =& a; // or int i = &a; i am not sure about the syntax.
      It doesn't matter. Whitespace before and after the & operator is ignored.
      i = 20; // now both a and i are 20
      >
      so this type of reference is an implicit pointer... it points to a,
      but you don't use the way in C (int *pi = &a) And when you use (i = 20),
      it does the dereference silently. (*pi = 20;)
      >
      so a reference is new: a pointer but "looks like not a pointer".
      A reference in C++ is another name for an already existing object. After its
      initialization, it behaves just like the object it refers to.
      So from this point on, a reference and a pointer are not the same... a
      reference is a pointer "that doesn't look like a pointer."
      >
      they both points to something. but the syntax (or grammar) of usage
      doesn't look like it is a pointer in the C era. a reference is an
      "automatica lly dereferenced" pointer, shall we say? or an "implicit"
      pointer, or "silent" pointer.
      No, it isn't. A pointer can be null, a reference can't. A pointer can be
      uninitialized, a reference can't.
      In language we use nowadays, which language has "reference" to a
      "reference" ?
      That's another thing you can't have in C++. You can have a pointer to
      pointer, but you can't have a reference to reference, because references
      are not objects (unlike pointers).


      Comment

      • Stuart Golodetz

        #4
        Re: Pointer vs Reference

        "Joshua Cranmer" <Pidgeot18@veri zon.netwrote in message
        news:VUtLi.1941 $1d2.688@trndny 05...
        Summercool wrote:
        >Can we confirm the following? also someone said, Java also has
        >"reference" like in C++, which is an "implicit pointer":
        >>
        >Pointer and Reference
        >---------------------
        >>
        >I am starting to see what pointer and reference are and how they
        >relate
        >to each other.
        >>
        >in the C era, a pointer *is* a reference. that's why when we have
        >>
        >int a = 10;
        >int *pi = &a;
        >>
        >and you can "dereferenc e it":
        >>
        >*pi = 20;
        >>
        >Until when C++ comes along, then we have a new "reference" :
        >>
        >int a = 10;
        >int i =& a; // or int i = &a; i am not sure about the syntax.
        >
        int i = &a; is the syntax.
        ITYM int& i = a;

        Best wishes,
        Stu

        <snip>


        Comment

        • Steve Folly

          #5
          Re: Pointer vs Reference

          On 29/9/07 16:09, in article VUtLi.1941$1d2. 688@trndny05, "Joshua Cranmer"
          <Pidgeot18@veri zon.netwrote:
          Summercool wrote:
          >Can we confirm the following? also someone said, Java also has
          >"reference" like in C++, which is an "implicit pointer":
          >>
          >Pointer and Reference
          >---------------------
          >>
          >I am starting to see what pointer and reference are and how they
          >relate
          >to each other.
          >>
          >in the C era, a pointer *is* a reference. that's why when we have
          >>
          >int a = 10;
          >int *pi = &a;
          >>
          >and you can "dereferenc e it":
          >>
          >*pi = 20;
          >>
          >Until when C++ comes along, then we have a new "reference" :
          >>
          >int a = 10;
          >int i =& a; // or int i = &a; i am not sure about the syntax.
          >
          int i = &a; is the syntax. Yes, the ampersand is used for both reference
          and address-of, so it can be confusing (especially since I think that i
          would be set to the address of a in C).
          Wrong. I think what you probably meant is

          int& i = a;

          Yes, the ampersand is used for reference and address-of - when used as part
          of a type, it's a reference, when used in an expression it's the address-of
          operator.

          Let me start by asking a question to the C++ language lawyers out there:
          what should this print out:
          Compilation error?


          --
          Regards,
          Steve

          "...which means he created the heaven and the earth... in the DARK! How good
          is that?"

          Comment

          • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

            #6
            Re: Pointer vs Reference

            On 2007-09-29 16:32, Summercool wrote:
            Can we confirm the following? also someone said, Java also has
            "reference" like in C++, which is an "implicit pointer":
            Java have references, yes. But they are not the same thing as C++
            references, neither when it comes to implementation or semantics.
            Pointer and Reference
            ---------------------
            >
            I am starting to see what pointer and reference are and how they
            relate
            to each other.
            >
            in the C era, a pointer *is* a reference. that's why when we have
            No, a pointer is a pointer. They can be used to implement reference
            semantics (as opposed to value semantics).
            int a = 10;
            int *pi = &a;
            >
            and you can "dereferenc e it":
            >
            *pi = 20;
            >
            Until when C++ comes along, then we have a new "reference" :
            >
            int a = 10;
            int i =& a; // or int i = &a; i am not sure about the syntax.
            i = 20; // now both a and i are 20
            >
            so this type of reference is an implicit pointer... it points to a,
            No, think about C++ references as aliases for the type they refer to.
            One big difference between references and pointers is that references do
            not have an address, while pointers do. Consider the following small
            program:

            int main() {
            int a = 5;
            int& b = a;
            int* c = &a;
            }

            In a totally un-optimised program there is no guarantee that any more
            memory than sizeof(int) + sizeof(int*) is allocated for variables in main().
            but
            you don't use the way in C (int *pi = &a) And when you use (i = 20),
            it does the dereference silently. (*pi = 20;)
            >
            so a reference is new: a pointer but "looks like not a pointer".
            One important difference between a pointer and a reference (in C++) is
            that a pointer can be made to point at any object of the right type,
            while a reference always points to only one object. In other words a
            reference can not be reseated.

            This is one of the bigger differences between C++ and Java references,
            Java references can be reseated and are in that way more like a C/C++
            pointer. However notice that a Java reference is not a pointer, it can
            not be used to perform pointer arithmetic operations among other
            differences.
            come to think about it, in Java and Ruby, they are like that too.
            >
            a = Car.new
            >
            a doesn't look like a pointer, but it is actually a pointer.
            >
            we don't dereference it to get to the attributes like (*a).value = 10
            or a->value = 10 but just use a.value = 10
            That probably means that it is a reference, and not a pointer.
            So from this point on, a reference and a pointer are not the same... a
            reference is a pointer "that doesn't look like a pointer."
            >
            they both points to something. but the syntax (or grammar) of usage
            doesn't look like it is a pointer in the C era. a reference is an
            "automatica lly dereferenced" pointer, shall we say? or an "implicit"
            pointer, or "silent" pointer.
            I prefer to call it a reference.


            [snip PHP which is off topic in both c.l.c++ and c.l.j.p]
            In language we use nowadays, which language has "reference"
            I would suspect that any language with OO support provides some kind of
            reference, and probably a few others to.
            In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.
            >
            In language we use nowadays, which language has "reference" to a
            "reference" ?
            >
            I think C++, Java, PHP5 do... not sure about Python and Ruby.
            C++ does not have references to references, consider the following code:

            int a;
            int& b = a; // b is a reference to a
            int& c = b; // c is a reference to a *not* b

            C++ do, however, have pointers to pointers, or references to pointers,
            but as previously pointed out pointers and references are entirely
            separate things.

            --
            Erik Wikström

            Comment

            • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

              #7
              Re: Pointer vs Reference

              On 2007-09-29 17:09, Joshua Cranmer wrote:
              Summercool wrote:
              >Can we confirm the following? also someone said, Java also has
              >"reference" like in C++, which is an "implicit pointer":
              >>
              >Pointer and Reference
              >---------------------
              >>
              >I am starting to see what pointer and reference are and how they
              >relate
              >to each other.
              >>
              >in the C era, a pointer *is* a reference. that's why when we have
              >>
              >int a = 10;
              >int *pi = &a;
              >>
              >and you can "dereferenc e it":
              >>
              >*pi = 20;
              >>
              >Until when C++ comes along, then we have a new "reference" :
              >>
              >int a = 10;
              >int i =& a; // or int i = &a; i am not sure about the syntax.
              >
              int i = &a; is the syntax. Yes, the ampersand is used for both reference
              and address-of, so it can be confusing (especially since I think that i
              would be set to the address of a in C).
              Actually they are both wrong, but you are party right that the & is the
              address-of operator. The above can be written either as

              int* i = &a; // i is a pointer

              or

              int& i = a; // i is a reference

              Notice that the & when used for a reference is part of the type (just
              like the * for a pointer).
              >i = 20; // now both a and i are 20
              >>
              >so this type of reference is an implicit pointer... it points to a,
              >
              I wouldn't call it an implicit pointer: you cannot, to my knowledge,
              change i to point to any other object but a like you can with a pointer.
              Rather, i is an alias of a: the two objects forever more point to the
              same thing.
              >
              >
              >so a reference is new: a pointer but "looks like not a pointer".
              >
              Not quite. See above.
              >
              >come to think about it, in Java and Ruby, they are like that too.
              >
              No, it is not. In syntax, Java more nearly follows the reference syntax
              but it is closer to a pointer with transparent {de}referencing .
              >
              >In language we use nowadays, which language has "reference"
              >>
              >In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.
              >>
              >In language we use nowadays, which language has "reference" to a
              >"reference" ?
              >>
              >I think C++, Java, PHP5 do... not sure about Python and Ruby.
              >>
              >
              Let me start by asking a question to the C++ language lawyers out there:
              what should this print out:
              >
              int a = 50, b = 20;
              int i = &a;
              Assuming you meant i to be a reference here (int& i = a;).
              std::cout << "a: " << a << " b: " << b << " i: " << i;
              a: 50 b: 20 i: 50
              i = 30;
              std::cout << "a: " << a << " b: " << b << " i: " << i;
              a: 30 b: 20 i: 30
              i = &b;
              i = b; // Cannot reseat a reference
              std::cout << "a: " << a << " b: " << b << " i: " << i;
              a: 20 b: 20 i: 20
              b = &a;
              b = i;
              std::cout << "a: " << a << " b: " << b << " i: " << i;
              Same as above
              a = &i; // Creating a circular loop?
              a = i; // Same as "a = a;"
              std::cout << "a: " << a << " b: " << b << " i: " << i;
              Same as above

              --
              Erik Wikström

              Comment

              • Rolf Magnus

                #8
                Re: Pointer vs Reference

                Rolf Magnus wrote:
                >int i =& a; // or int i = &a; i am not sure about the syntax.
                >
                It doesn't matter. Whitespace before and after the & operator is ignored.
                Oops. Even though my answer is right, this is - as others have pointed out -
                not the correct syntax for defining a reference.

                Comment

                • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                  #9
                  Re: Pointer vs Reference

                  On 2007-09-29 17:32, Stefan Ram wrote:
                  Summercool <Summercoolness @gmail.comwrite s:
                  >>in the C era, a pointer *is* a reference. that's why when we have
                  >
                  In Java, reference values are pointers.
                  All references are pointers, but none are the kind of pointers usually
                  meant when discussing C or C++. For example in C and C++ if you have a
                  pointer p and do "p++;" that is an operation on the pointer. Whereas in
                  Java, if you have a reference r and do "r++;" that would be an operation
                  on the object r refers to (provided that Java supports operator
                  overloading, I can not remember if it does).

                  --
                  Erik Wikström

                  Comment

                  • Joshua Cranmer

                    #10
                    Re: Pointer vs Reference

                    Erik Wikström wrote:
                    Assuming you meant i to be a reference here (int& i = a;).
                    I did.
                    i = b; // Cannot reseat a reference
                    And that was what I was most interested about. It was also what I suspected.
                    --
                    Beware of bugs in the above code; I have only proved it correct, not
                    tried it. -- Donald E. Knuth

                    Comment

                    • Summercool

                      #11
                      Re: Pointer vs Reference

                      On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia. comwrote:
                      >
                      All references are pointers, but none are the kind of pointers usually
                      meant when discussing C or C++. For example in C and C++ if you have a
                      pointer p and do "p++;" that is an operation on the pointer. Whereas in
                      Java, if you have a reference r and do "r++;" that would be an operation
                      on the object r refers to [...]
                      So can we think of a C++ reference as:

                      Same as a pointer, 4 bytes.
                      Same as a pointer, points to some where.

                      Main difference: with pointer, you can print out the pointer, set the
                      pointer to different values, including 0. With reference, you can't.
                      You always dereference a reference when you use it.

                      Here are some C++ and C code equivalent:

                      int a = 10; // In C: the same
                      int avg = 20; // In C: the same

                      int& b = a; // In C: int *pa = &a;
                      printf "%d", b; // In C: printf "%d", *pa;
                      b = 20; // In C: *pa = 20;
                      // can't do // In C: pa = (int *) 0; or (int *) NULL;
                      // can't do // In C: pa = &avg;

                      int& c = b; // In C: int *pa2 = &(*pa)
                      // &(*pa) is &(20) which is illegal
                      // but if it is C++, it magically
                      // changes &(*pa) to just pa
                      // So in C, int *pa2 = pa;


                      So In Java, Python, PHP5, and Ruby, when you use

                      a = Dog.new

                      it is really not a reference, not a pointer, but
                      something in between.

                      Not a reference, because you can set where it points to:

                      a = nil

                      Not a pointer, because you use

                      a.bark()

                      instead of a->bark() or (*a).bark()



                      Comment

                      • Joshua Cranmer

                        #12
                        Re: Pointer vs Reference

                        Summercool wrote:
                        On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia. comwrote:
                        >All references are pointers, but none are the kind of pointers usually
                        >meant when discussing C or C++. For example in C and C++ if you have a
                        >pointer p and do "p++;" that is an operation on the pointer. Whereas in
                        >Java, if you have a reference r and do "r++;" that would be an operation
                        >on the object r refers to [...]
                        >
                        So can we think of a C++ reference as:
                        >
                        Same as a pointer, 4 bytes.
                        Same as a pointer, points to some where.
                        Or the best way I have heard a reference describe is that you are
                        specifying another name to the same variable. The term reference is,
                        IMO, poorly named: a better name would be alias.
                        So In Java, Python, PHP5, and Ruby, when you use
                        >
                        a = Dog.new
                        >
                        I presume this is Ruby syntax? It is definitely neither Java nor PHP5
                        and I do not think that it is Python either. Considering that your
                        thread is X-posted to c.l.j.p and c.l.c++, you might want to compare C++
                        and Java syntax. Just a suggestion, though.
                        it is really not a reference, not a pointer, but
                        something in between.
                        In Java, it really is a pointer. The JLS actually specifically says that
                        reference values are pointers, as Stefan pointed out.
                        Not a pointer, because you use
                        >
                        a.bark()
                        >
                        instead of a->bark() or (*a).bark()
                        Who says that people have to use C-syntax for method dispatches? Since
                        the actual value of the pointer cannot be extracted, why put people
                        through syntactic hoops just to call a method? It *is* a pointer, *by
                        definition.*

                        I can show you another syntax that operates on pointers:

                        mov [eax], 5

                        Wow, I said "move five to the value of eax" without writing
                        *eax = 5;

                        Does that mean that eax is not a pointer but something similar to one?


                        P.S. I somewhat apologize for the sarcasm, but your posts do have a
                        characteristic of XXX is the right way and therefore everything else is
                        wrong.

                        --
                        Beware of bugs in the above code; I have only proved it correct, not
                        tried it. -- Donald E. Knuth

                        Comment

                        • Summercool

                          #13
                          Re: Pointer vs Reference

                          On Sep 29, 9:57 am, Joshua Cranmer <Pidgeo...@veri zon.netwrote:
                          Not a pointer, because you use
                          >
                          a.bark()
                          >
                          instead of a->bark() or (*a).bark()
                          >
                          Who says that people have to use C-syntax for method dispatches? Since
                          the actual value of the pointer cannot be extracted, why put people
                          through syntactic hoops just to call a method? It *is* a pointer, *by
                          definition.*
                          Now... so in Java, Python, PHP5, and Ruby, it looks like they are all
                          pointers when you say

                          a = new Dog("woofy")

                          or

                          a = Dog.new("woofy" )

                          so they are all pointers, not reference. (because reference cannot
                          point to a different thing after it is set, like a = new Dog("lulu")
                          or a = nil)

                          the syntax a.color or a.bark is just a simpler way of writing C or C
                          ++'s "->"

                          so that's it? I tend to compare the "." and the "->" as I view the
                          relatively modern language having similar syntax or operators...
                          (didn't expect "." to mean "->" in another language)


                          Comment

                          • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                            #14
                            Re: Pointer vs Reference

                            On 2007-09-29 18:39, Summercool wrote:
                            On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia. comwrote:
                            >>
                            >All references are pointers, but none are the kind of pointers usually
                            >meant when discussing C or C++. For example in C and C++ if you have a
                            >pointer p and do "p++;" that is an operation on the pointer. Whereas in
                            >Java, if you have a reference r and do "r++;" that would be an operation
                            >on the object r refers to [...]
                            >
                            So can we think of a C++ reference as:
                            >
                            Same as a pointer, 4 bytes.
                            Same as a pointer, points to some where.
                            No, a pointer will always have an address, which you can print out or
                            whatever. Since it has an address it will also take up some memory
                            (either on the stack or on the heap). A reference does not have an
                            address and does not require memory to be allocated.

                            Another issue is how the compiler implements the semantics of a
                            reference, in some cases it will require allocating memory, but in some
                            cases it will not. For a pointer it will always allocate memory since
                            the standard requires that it does so. (All the above modulo any
                            optimisations).
                            Main difference: with pointer, you can print out the pointer, set the
                            pointer to different values, including 0. With reference, you can't.
                            You always dereference a reference when you use it.
                            The best way to understand the difference between a pointer and a
                            reference is to not try to compare them. They are two completely
                            separate concepts.
                            Here are some C++ and C code equivalent:
                            >
                            int a = 10; // In C: the same
                            int avg = 20; // In C: the same
                            >
                            int& b = a; // In C: int *pa = &a;
                            printf "%d", b; // In C: printf "%d", *pa;
                            b = 20; // In C: *pa = 20;
                            // can't do // In C: pa = (int *) 0; or (int *) NULL;
                            // can't do // In C: pa = &avg;
                            >
                            int& c = b; // In C: int *pa2 = &(*pa)
                            // &(*pa) is &(20) which is illegal
                            No, &(*pa) == pa, or put another way, you first dereferences the
                            pointer, and then takes the address of the integer returned. The address
                            of the int is then a int* pointing to the same place as pa.

                            --
                            Erik Wikström

                            Comment

                            • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                              #15
                              Re: Pointer vs Reference

                              On 2007-09-29 18:57, Joshua Cranmer wrote:
                              Summercool wrote:
                              >On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia. comwrote:
                              >>All references are pointers, but none are the kind of pointers usually
                              >>meant when discussing C or C++. For example in C and C++ if you have a
                              >>pointer p and do "p++;" that is an operation on the pointer. Whereas in
                              >>Java, if you have a reference r and do "r++;" that would be an operation
                              >>on the object r refers to [...]
                              >>
                              >So can we think of a C++ reference as:
                              >>
                              >Same as a pointer, 4 bytes.
                              >Same as a pointer, points to some where.
                              >
                              Or the best way I have heard a reference describe is that you are
                              specifying another name to the same variable. The term reference is,
                              IMO, poorly named: a better name would be alias.
                              >
                              >So In Java, Python, PHP5, and Ruby, when you use
                              >>
                              >a = Dog.new
                              >>
                              >
                              I presume this is Ruby syntax? It is definitely neither Java nor PHP5
                              and I do not think that it is Python either. Considering that your
                              thread is X-posted to c.l.j.p and c.l.c++, you might want to compare C++
                              and Java syntax. Just a suggestion, though.
                              >
                              >it is really not a reference, not a pointer, but
                              >something in between.
                              >
                              In Java, it really is a pointer. The JLS actually specifically says that
                              reference values are pointers, as Stefan pointed out.
                              Just to clarify: it is a pointer, but not a C/C++ pointer. The closest
                              thing you get a Java reference in C++ is probably a struct wrapping a
                              pointer to the object and overloading the . operator so that it performs
                              a null-pointer check.

                              --
                              Erik Wikström

                              Comment

                              Working...