Why pointers?

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

    Why pointers?

    I have written C++ code for some time now using both pointers and
    references.

    I was asked what the point was in using pointers and could not give a
    short an explanatory example.

    I would say that you use a pointer when data are updated across function
    calls.

    But does anyone have an idea to a minimal example where its impossible
    to avoid pointer?
  • pastbin@gmail.com

    #2
    Re: Why pointers?

    [8.6] When should I use references, and when should I use pointers?

    Comment

    • Szabolcs Ferenczi

      #3
      Re: Why pointers?

      On Feb 13, 10:57 pm, saneman <y...@dd.comwro te:
      But does anyone have an idea to a minimal example where its impossible
      to avoid pointer?
      Think about polymorphism in C++.

      Best Regards,
      Szabolcs

      Comment

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

        #4
        Re: Why pointers?

        On 2008-02-13 22:57, saneman wrote:
        I have written C++ code for some time now using both pointers and
        references.
        >
        I was asked what the point was in using pointers and could not give a
        short an explanatory example.
        >
        I would say that you use a pointer when data are updated across function
        calls.
        >
        But does anyone have an idea to a minimal example where its impossible
        to avoid pointer?
        In any situation where you need to be able to "reseat" the reference. On
        example would be walking through the nodes in a linked list without
        using recursion, something like:

        node* n;
        while (n != 0)
        {
        // do something with n
        n = n->next;
        }

        --
        Erik Wikström

        Comment

        • Salt_Peter

          #5
          Re: Why pointers?

          On Feb 13, 5:24 pm, Szabolcs Ferenczi <szabolcs.feren ...@gmail.com>
          wrote:
          On Feb 13, 10:57 pm, saneman <y...@dd.comwro te:
          >
          But does anyone have an idea to a minimal example where its impossible
          to avoid pointer?
          >
          Think about polymorphism in C++.
          >
          Best Regards,
          Szabolcs
          Polymorphism works quite nicely with references too.

          Comment

          • Juha Nieminen

            #6
            Re: Why pointers?

            saneman wrote:
            But does anyone have an idea to a minimal example where its impossible
            to avoid pointer?
            Try creating a linked list without pointers.

            Comment

            • Lionel B

              #7
              Re: Why pointers?

              On Thu, 14 Feb 2008 14:00:10 +0200, Juha Nieminen wrote:
              saneman wrote:
              >But does anyone have an idea to a minimal example where its impossible
              >to avoid pointer?
              >
              Try creating a linked list without pointers.
              Sure it's possible... clunky, painful and probably inefficient, but
              possible (e.g. use arrays for storage and arrays of indices for access).

              --
              Lionel B

              Comment

              • Pete Becker

                #8
                Re: Why pointers?

                On 2008-02-14 10:35:15 -0500, Lionel B <me@privacy.net said:
                On Thu, 14 Feb 2008 14:00:10 +0200, Juha Nieminen wrote:
                >
                >saneman wrote:
                >>But does anyone have an idea to a minimal example where its impossible
                >>to avoid pointer?
                >>
                >Try creating a linked list without pointers.
                >
                Sure it's possible... clunky, painful and probably inefficient, but
                possible (e.g. use arrays for storage and arrays of indices for access).
                But given that it's hypothetically impossible to give such an example
                (a Turing machine doesn't have pointers), examples that are clunky,
                painful, and inefficient without pointers are good answers.

                --
                Pete
                Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                Standard C++ Library Extensions: a Tutorial and Reference
                (www.petebecker.com/tr1book)

                Comment

                • Daniel T.

                  #9
                  Re: Why pointers?

                  saneman <yyyy@dd.comwro te:
                  Sorry for the confusion. I would like an example where its impossible to
                  avoid use 'new' (dynamic allocation).
                  >
                  The reason is that I often use pointers and references but seldom the
                  keyword 'new'.
                  You seldom use the keyword 'new'? Sounds like you have answered your own
                  question. Try to go back and do the same thing without the keyword. :-)

                  In C++, you should be using 'new' very rarely.

                  Comment

                  • arnuld

                    #10
                    Re: Why pointers?

                    On Fri, 15 Feb 2008 07:19:02 -0500, Daniel T. wrote:
                    In C++, you should be using 'new' very rarely.
                    Oh.. I did not know that :(



                    --


                    Comment

                    Working...