reference type methods

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

    #16
    Re: reference type methods

    On Jul 15, 1:37 pm, Ruben <ru...@www2.mrb rklyn.comwrote:
    On Tue, 15 Jul 2008 02:32:48 -0700, James Kanze wrote:
    No. By definition, i[index] is *(i + index), and the
    results of a dereference operator is always an lvalue.
    There's no literal involve here at all.
    Can you show me that definition, because I've had a whole
    other conversation in the comp.lang.c that was supported by
    compiler output and the C FAQ that proved this to be untrue.
    I think you're confusing things. An array is not a pointer,
    that's certain. But an expression with an array type will
    implicitly convert to a ponter. And since the [] is only
    defined for pointers (not for arrays), that's what happens here.

    See section 4.2 in the C++ standard for the array to pointer
    conversion, and 5.2.1 for the definition of the [] operator (or
    sections 6.3.2.1 and 6.5.2.1 respectively in the C standard).
    An array object is not just a simple pointer. And when I've
    worked with bearwolf cluster programming, this is explicitely
    clear.
    Who said it was? But since the [] operator is not defined for
    array types, the array converts to a pointer.
    With regard to the lvalue of a valuable's data,
    your saying that
    int x = 10;
    retunr x;
    that returns an lvalue? 10 is not an lvalue. So I've
    confused. If this was true then I would be able to do with
    No. The expression x is an lvalue. It is used to initialize
    the return value (which can be thought of as a hidden temporary
    object), which involves an lvalue to rvalue conversion.

    And of course, 10 is not an lvalue, but 10 isn't x.
    int f(x);
    f(x) = 5; //that can't be done
    Of course not. A function expression is an lvalue if and only
    if its return type is a reference.
    I think we are mixing up the data stored in variables from the
    variables themselves.
    I think you really need a basic course in C/C++ expressions.
    Lvalue-ness applies to expressions, not values. And because of
    the lvalue to rvalue conversion, an expression which is an
    lvalue can be used where ever an rvalue is required. (The
    reverse is not true, of course.)

    --
    James Kanze (GABI Software) email:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    • Eugene Vernikovskiy

      #17
      Re: reference type methods

      I think you can simplify things with references a lot if you realize
      that there is no significant difference between references and pointers.

      For example

      int iX = 10;

      int& rX = iX;
      int* pX = &iX;

      In both cases we save an address of the variable iX in a reference or in
      a pointer. So

      rX = 5; // This makes the expression iX == 5 true
      *pX = 6; // This makes the expression iX == 6 true

      Then think about the function

      int* fPointer() { return &iX; }

      By all means you realize that

      *fPointer() = 5; // This makes the expression iX == 5 true;

      Why are you surprized that the following is correct?

      int& fRef() { return iX; }

      fRef() = 6; // This makes the expression iX == 6 true;

      There is no difference between rX and fRef() in this case. fRef returns
      not the rValue but the lValue which can be modified (the same way as the
      pointer value can be used to modify the value it is referencing to).

      Now if you accept that a[n] = 5 puts the value 5 into the n+1's element
      of the array you should accept the syntax below:

      int& f( int n ) { return a[n]; }
      f(n) = 5;

      Obviously f(n) and a[n]is just the same thing. You can look on the
      pointers if you want, it is just the same:

      int* f( int n ) { return a + n; } // &a[n] and a + n is the same
      *f(n) = 5;


      P.S.
      a[5] cannot be an r-value. It is an l-value. Otherwise a[5] = 6 is a
      syntax error.

      Comment

      • James Kanze

        #18
        Re: reference type methods

        On Jul 17, 5:10 am, Eugene Vernikovskiy
        <eugene.verniko vs...@verizon.n etwrote:
        I think you can simplify things with references a lot if you
        realize that there is no significant difference between
        references and pointers.
        Oh yes there are. To begin with, references aren't objects, and
        pointers are. What you probably meant to say is that references
        behave very much like automatically dereferenced pointers.
        For example
        int iX = 10;
        int& rX = iX;
        int* pX = &iX;
        In both cases we save an address of the variable iX in a
        reference or in a pointer.
        That's one possible implementation. But the semantics of
        references have been carefully specified so that they could be
        implemented as pointers. (In such direct cases, they usually
        aren't. But they could be.)

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Ruben

          #19
          Re: reference type methods

          On Thu, 17 Jul 2008 03:10:34 +0000, Eugene Vernikovskiy wrote:
          *fPointer() = 5; // This makes the expression iX == 5 true;
          What akes this snta legal?

          Ruben

          --
          http://www.mrbrklyn.com - Interesting Stuff
          http://www.nylxs.com - Leadership Development in Free Software

          So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

          http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

          "Yeah - I write Free Software...so SUE ME"

          "The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

          "I'm an engineer. I choose the best tool for the job, politics be damned.<
          You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

          © Copyright for the Digital Millennium

          Comment

          • Eugene Vernikovskiy

            #20
            Re: reference type methods

            Ruben wrote:
            On Thu, 17 Jul 2008 03:10:34 +0000, Eugene Vernikovskiy wrote:
            >
            >*fPointer() = 5; // This makes the expression iX == 5 true;
            >
            What akes this snta legal?
            >
            Ruben
            >
            Lets reset the example:

            1)
            int iX = 0;
            int* fPointer() { return &iX; }

            void f() { *fPointer() = 5; // Puts 5 into iX;
            }


            If you are surprised that it works this way, why do you think that the
            example below works fine:

            2)
            int iX = 0;
            int* pX = &iX;

            void f() { *pX = 5; // Puts 5 into iX
            }

            If you understand that the second example works then reconstruct the
            first example as:

            3)
            int iX = 0;
            int* fPointer() { return &iX; }
            int* pX = fPointer();

            void f() { *pX = 5; // Puts 5 into iX
            }

            Notice that it is not necessary to assign address returned by the
            function into a pointer. It is possible to use this address right away
            as in the second example. Each expression which has a type of a pointer
            can be dereferenced. For example

            int a[10];

            *(a+5) = 10; // It is the same as a[5] = 10

            Same as

            int* p = a; // or &a[0]
            *(p+5) = 10; // It is the same as a[5] = 10


            Perhaps you misunderstand the meaning of the operation *p. *p is an
            l-value which can be used on the left side of the assignment. If *p is
            used on the right site of the assignment then it is converter from
            l-value to r-value first and then the compiled r-value is used. (I am
            not talking about the rules of overloaded operators though, just plain C)

            Comment

            • Eugene Vernikovskiy

              #21
              Re: reference type methods

              James Kanze wrote:
              On Jul 17, 5:10 am, Eugene Vernikovskiy
              <eugene.verniko vs...@verizon.n etwrote:
              >I think you can simplify things with references a lot if you
              >realize that there is no significant difference between
              >references and pointers.
              >
              Oh yes there are. To begin with, references aren't objects, and
              pointers are. What you probably meant to say is that references
              behave very much like automatically dereferenced pointers.
              >
              Yes. My intention was to stress out the similarity between references
              and pointers because each C programmer (who knows about pointers) can
              start understanding references by assuming that they are automatically
              dereferenced pointers.

              I do not think that introducing the concept of objects in this
              conversation can help understanding the issue. The first step is to
              understand similarities and then we can start talking about the
              differences :-)

              Comment

              • James Kanze

                #22
                Re: reference type methods

                On Jul 18, 1:48 am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
                Eugene Vernikovskiy <eugene.verniko vs...@verizon.n etwrites:
                James Kanze wrote:
                >Oh yes there are. To begin with, references aren't objects,
                >and pointers are. What you probably meant to say is that
                >references behave very much like automatically dereferenced
                >pointers.
                Yes. My intention was to stress out the similarity between
                references and pointers because each C programmer (who knows
                about pointers) can start understanding references by
                assuming that they are automatically dereferenced pointers.
                I do not think that introducing the concept of objects in this
                FWIW: I didn't really want to disagree too much with Eugene,
                because pointing out how pointers and references are similar can
                help understanding. But I do think it important to signal from
                the start that there are also important differences: references
                aren't pointers, even if they behave like pointers in certain
                contexts.
                I do not agreee with several of the statements above. But
                since I do not want to write too much now and already wrote
                about references before, I will just choose one example:
                James Kanze writes above:
                »pointers are objects.«
                Usually, pointers are /not/ objects.
                A pointer is, by definition, an object, according to the C++
                standard. Most importantly, it has the essential
                characteristics of an object: it occupies space and has an
                address. Neither of which is (necessarily or conceptuallY) true
                of a reference.

                It is, IMHO, a very important distinction between pointers and
                references. You can take the address of a pointer, but not of a
                reference. The sizeof operator on a pointer returns the size of
                the pointer; the sizeof operator on a reference returns the size
                of the referenced object.

                In the context the original poster raised, references behave
                very much like pointers, as Eugene explained very clearly. In
                other contexts, however: a pointer is a first class object in
                C++ (more so, even, than an array); a reference isn't an object
                at all.
                For example:
                #include <iostream>
                #include <ostream>
                int main()
                { char const * const a = "abc";
                ::std::cout << *( a + 1 ); }
                b
                (The last line is the output.)
                The dereferencing operator »*« requires a pointer operand.
                Therefore, »a + 1« is a pointer.
                But »a + 1« is not an l-value (you cannot write »( a + 1 )=...«),
                so this pointer is not an object. (It /refers/ to an object, but
                it is not an object itself.)
                What you're saying holds equally well for int or double. »a +
                1« is an expression which has pointer type. It's not a pointer
                object. On the other hand, anything you declare as a pointer is
                an object. There is no such thing as a reference which is an
                object.
                It is a value (an r-value).
                I have heard this misconception that pointers were objects
                so often that I wonder how so many people pick up this idea.
                It is, I admit, a somewhat careless use of language. There are
                really two different ways the word "pointer" (or of »int«, etc.)
                are used. One is purely type: an expression has type int, or it
                has a pointer type. In the other, we tend to use the work to
                mean something declared with that type: i is an int (not "a
                variable with type int"), p is a pointer (not "a variable with
                pointer type"). From there, it is a simple step to say that
                "pointers (ints) are objects", instead of the more precise
                "entities declared with pointer type (type int) are objects",
                which is, of course, what is really meant. Usually, the context
                is such that the actual meaning is clear. (But I'm not sure
                that this is the case here, and I should have been clearer.)

                Note that even the standard isn't always as clear as one might
                like. I think, however, that data types can be divided into
                four categories (within expressions, in this regard):

                -- the fundamental types, enumeration types, pointers and
                pointers to members: objects when they are lvalues, not when
                they are rvalues;

                -- class types (including unions!): always objects, whether
                lvalue or rvalue;

                -- arrays: always objects, but can only exist as lvalues (there
                are no expressions which result in an rvalue array); and

                -- references: never objects.

                Using, of course, the definition of object in the C++ standard:
                "An object is a region of storage."

                Personally, I would rather see fewer categories, with all of the
                first three behaving like class types (formally---the lack of
                member functions would mean that a compiler could always
                "optimize out" the actual object of an rvalue of fundamental
                type). Historically, however: we inherit the first and third
                points from C, and when you add member functions and a this
                pointer, even rvalue class types have to be objects. So we're
                stuck with three categories where, were the language defined
                from scratch, one would do.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • Ruben

                  #23
                  Re: reference type methods

                  On Thu, 17 Jul 2008 23:02:49 +0000, Eugene Vernikovskiy wrote:
                  If you are surprised that it works this way, why do you think that the
                  example below works fine:
                  YES - I'm astonished. Would this work in C? How is it interpreted?

                  Ruben

                  --
                  http://www.mrbrklyn.com - Interesting Stuff
                  http://www.nylxs.com - Leadership Development in Free Software

                  So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

                  http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

                  "Yeah - I write Free Software...so SUE ME"

                  "The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

                  "I'm an engineer. I choose the best tool for the job, politics be damned.<
                  You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

                  © Copyright for the Digital Millennium

                  Comment

                  Working...