why reference ?

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

    #16
    Re: why reference ?


    [color=blue]
    > Actually, my code will not throw any exception, the whole thing will just crash. On Linux, probably the famous "Segmentati on
    > fault. Core dumped."[/color]

    It's embarrassing that a coder presents code that "just crashs". Do you know
    exception handling? Remember, the topic of this thread-child is that pointers
    are needless. You showed us a crashy solution with a pointer.
    Shake your head and wake up!

    Thomas B.



    Comment

    • Arijit

      #17
      Re: why reference ?

      Thomas Barth wrote:[color=blue][color=green]
      >>Actually, my code will not throw any exception, the whole thing will just crash. On Linux, probably the famous "Segmentati on
      >>fault. Core dumped."[/color]
      >
      >
      > It's embarrassing that a coder presents code that "just crashs". Do you know
      > exception handling? Remember, the topic of this thread-child is that pointers
      > are needless. You showed us a crashy solution with a pointer.
      > Shake your head and wake up!
      >
      > Thomas B.
      >[/color]

      I know exception handling. The point is that no exceptions will be
      thrown in this case. And I showed a solution without explicit pointers.
      No out-of-bound exception will be generated, even when you access
      elements outside the array bounds.

      And you miss the whole point of my reply. The message I replied to had
      this ->
      [color=blue]
      >double GetFirstNegativ e(double* pNums)
      >{
      > while(*pNums>0. 0) ++pNums;
      >return *pNums;
      >}
      >
      >Try that with references and compare the assembly generated.[/color]

      Which I changed to ->
      [color=blue]
      >double GetFirstNegativ e(double* pNums)
      >{
      > int i=0;
      > while(pNums[i]>0.0)
      > ++i;
      > return pNums[i];
      >}
      >
      >No pointers, no references. Agreed, has an extra variable.[/color]

      I probably should have written double GetFirstNegativ e(double pNum[]),
      but thats besides the point. What I tried to do was the change the code
      already posted to work without pointers, while keeping the structure of
      the original code unaltered. Thats why I did not use range checking,
      because the original code didn't have it. And the way the code is
      written, range checking is unnecessary if you use sentinels.


      -Arijit

      Comment

      • Thomas Barth

        #18
        Re: why reference ?

        >[color=blue]
        > I probably should have written double GetFirstNegativ e(double pNum[]), but thats besides the point. What I tried to do was the
        > change the code already posted to work without pointers, while keeping the structure of the original code unaltered. Thats why I
        > did not use range checking,[/color]

        Who said to keep the structure of the code? :-) Of course you have
        to change the programming style as well, if you want to pass on pointers.
        That's the deal with it.

        Have a nice evening :)

        Thomas B.



        Comment

        • Markus Elfring

          #19
          Re: why reference ?

          > Are there any efficiency differences ?

          Do you check the pointer in g() for a non-zero value?
          This safety check can be avoided and omitted by the passing of the
          arguments by reference.

          Comment

          • Old Wolf

            #20
            Re: why reference ?

            "Thomas Barth" <th.barth@mail. isis.de> wrote:
            Arijit wrote:
            [color=blue]
            >[color=green]
            > >
            > > double GetFirstNegativ e(double* pNums)
            > > {
            > > int i=0;
            > > while(pNums[i]>0.0)
            > > ++i;
            > > return pNums[i];
            > > }
            > >
            > > No pointers, no references. Agreed, has an extra variable.[/color][/color]

            This still uses a pointer. pNums is a pointer. The fact that
            there is no '*' symbol in the code does not change this.
            pNums[i] is the same as: *(pNums + i)
            [color=blue]
            > Pointer free, array passed by reference
            >
            > double getFirstNegativ e(double arr[]) {[/color]

            In this case, arr is a pointer. This is identical to the
            original declaration.

            To pass an array by reference you need to write:
            double foo( double (&arr)[10] )

            Comment

            • E. Robert Tisdale

              #21
              Re: why reference ?

              William Xuuu wrote:
              [color=blue]
              > References introduced make things much more complicated. e.g.
              >
              > int a;
              > void f(int &n);
              > void g(int *p);[/color]

              Don't do that!

              Pass by value, const reference or const pointer
              and return by value:

              int f(const int& n) {
              return n + 1;
              }

              int g(const int* p) {
              return *p + 1;
              }
              [color=blue]
              > Isn't g(&a) much more clear than f(a),
              > When we would change the value of the lvalue a?
              >
              > Are there any efficiency differences?
              >
              > Is reference a must in C++?[/color]

              Comment

              • William Xuuu

                #22
                Re: why reference ?

                "John Harrison" <john_andronicu s@hotmail.com> writes:
                [color=blue]
                > Try writing an assignment operator using a pointer.
                >
                > class X
                > {
                > X& operator=(const X* rhs);
                > };
                >
                > Then you would then have to write
                >
                > X x, y;
                > x = &y;
                >
                > Do you think that is good style?[/color]

                Hmm, i see your point. But the assignment operator is different from a
                general method, we don't generally call it as x.operator=(&y) . Meanwhile in
                my e.g., i'd say g(&a) is better than f(a), right? So, there are some
                things in c++ that reference performs more elegant than pointer, not
                all. That's all.
                [color=blue]
                > References and pointers are different. The main difference is that a
                > reference once initialised cannot be made to refer to something else. This
                > is often exactly what you want, and makes using references very common, it
                > also makes the syntax simpler.[/color]

                One has to master two things now, compared with one before. I can't agree.

                --
                William Xuuu

                Comment

                • Cheng Mo

                  #23
                  How to set a C++ class final?

                  Hi,
                  I am new to C++.
                  In Java, it is possible to define one class to be "final" so no other
                  class can inherit it.
                  I am wondering whether or not there is some mechanism in C++ do the same
                  job. Besides putting all constructors privte, any other solution?

                  Comment

                  • Victor Bazarov

                    #24
                    Re: How to set a C++ class final?

                    "Cheng Mo" <mo.cheng@com.m otorola> wrote...[color=blue]
                    > I am new to C++.
                    > In Java, it is possible to define one class to be "final" so no other
                    > class can inherit it.
                    > I am wondering whether or not there is some mechanism in C++ do the same
                    > job. Besides putting all constructors privte, any other solution?[/color]

                    http://www.research.att.com/~bs/bs_f...#no-derivation


                    Comment

                    • John Harrison

                      #25
                      Re: why reference ?

                      >[color=blue][color=green]
                      >> References and pointers are different. The main difference is that a
                      >> reference once initialised cannot be made to refer to something else.
                      >> This
                      >> is often exactly what you want, and makes using references very common,
                      >> it
                      >> also makes the syntax simpler.[/color]
                      >
                      > One has to master two things now, compared with one before. I can't agree.[/color]

                      Fair enough, you're free not to use the parts of C++ that don't appeal to
                      you. You can just use them for parameters to overloaded operators and copy
                      constructors (where they are essential).

                      It took me along while to get used to references but now I use them all the
                      time.

                      john


                      Comment

                      • Ulf Wikstr?m

                        #26
                        Re: why reference ?

                        William Xuuu <william_xuuu@1 63.com> wrote in message news:<87y8h218k b.fsf@163.com>. ..[color=blue]
                        > Hi,
                        >
                        > References introduced make things much more complicated. e.g.
                        >
                        > int a;
                        > void f(int &n);
                        > void g(int *p);
                        >
                        > Isn't g(&a) much more clear than f(a), when we would change the value of
                        > the lvalue a ?
                        >
                        > Are there any efficiency differences ?
                        >
                        > Is reference a must in c++ ?
                        >
                        > thanks.[/color]

                        The reason that Stroustup introduced references into C++ was actually
                        operator overloading.
                        For example, in the assignment operator, the return type must be a
                        reference.

                        Comment

                        • Branimir Maksimovic

                          #27
                          Re: How to set a C++ class final?

                          Cheng Mo <mo.cheng@com.m otorola> wrote in message news:<cnedhp$gp s$2@avnika.corp .mot.com>...[color=blue]
                          > Hi,
                          > I am new to C++.
                          > In Java, it is possible to define one class to be "final" so no other
                          > class can inherit it.
                          > I am wondering whether or not there is some mechanism in C++ do the same
                          > job. Besides putting all constructors privte, any other solution?[/color]

                          // put this in header file:
                          // header_file.h
                          // -----------------------------
                          #ifndef HEADER_FILE_H
                          #define HEADER_FILE_H
                          class BaseOfFinalClas s{
                          public:
                          // ...some interface functions eg
                          virtual void function1()=0;
                          virtual ~BaseOfFinalCla ss(){}
                          };

                          BaseOfFinalClas s* makeFinalClass( );
                          #endif
                          // ------------------------------

                          // put your final class in implementation file:

                          // implementation_ file.cpp
                          // ------------------------
                          #include "header_fil e.h"
                          class FinalClass: public BaseOfFinalClas s{
                          public:
                          // ... implementation functions
                          virtual void function1(){ /* ... */ }
                          };

                          BaseOfFinalClas s* makeFinalClass( )
                          {
                          return new FinalClass;
                          }
                          // ------------------------

                          Greetings, Bane.

                          Comment

                          • Arijit

                            #28
                            Re: why reference ?

                            Old Wolf wrote:[color=blue]
                            > "Thomas Barth" <th.barth@mail. isis.de> wrote:
                            > Arijit wrote:
                            >
                            >[color=green][color=darkred]
                            >>>double GetFirstNegativ e(double* pNums)
                            >>>{
                            >>> int i=0;
                            >>> while(pNums[i]>0.0)
                            >>> ++i;
                            >>> return pNums[i];
                            >>>}
                            >>>
                            >>>No pointers, no references. Agreed, has an extra variable.[/color][/color]
                            >
                            >
                            > This still uses a pointer. pNums is a pointer. The fact that
                            > there is no '*' symbol in the code does not change this.
                            > pNums[i] is the same as: *(pNums + i)[/color]

                            I meant no explicit pointers are used. I indeed tried to write the
                            program without * . I consider using [] to dereference a pointer not
                            using pointers. You can never eliminate implicit pointers. For example,
                            references are usually implemented as pointers.
                            [color=blue][color=green]
                            >>Pointer free, array passed by reference
                            >>
                            >>double getFirstNegativ e(double arr[]) {[/color][/color]

                            This was not written by me.
                            [color=blue]
                            > In this case, arr is a pointer. This is identical to the
                            > original declaration.
                            >
                            > To pass an array by reference you need to write:
                            > double foo( double (&arr)[10] )[/color]

                            Yes. But will there be any difference in usage of arr in the two cases ?

                            -Arijit

                            Comment

                            • Default User

                              #29
                              Re: How to set a C++ class final?

                              Cheng Mo wrote:
                              [color=blue]
                              > Hi,
                              > I am new to C++.[/color]


                              I think you are new to newsgroups as well, as you posted your
                              completely irrelevant message in reply to someone else. Learn how to
                              use Thunderbird to start new threads rather than piggy-back on existing
                              ones.





                              Brian

                              Comment

                              Working...