pointer vs reference

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

    pointer vs reference

    what are the difference between the following 4 variables?

    const int * start
    int* const start
    int const& start
    Request(int const& start);
  • Jim Langston

    #2
    Re: pointer vs reference

    Eric Kaplan wrote:
    what are the difference between the following 4 variables?
    Read right to left.
    const int * start
    start is a pointer to an int that is constant (the int doesn't change).
    int* const start
    start is a constant pointer to an int (the pointer doesn't change)
    int const& start
    start is a reference to a constant int (the int doesn't change).
    Request(int const& start);
    start is a reference to a constant int (the int doesn't change).

    --
    Jim Langston
    tazmaster@rocke tmail.com


    Comment

    • Eric Kaplan

      #3
      Re: pointer vs reference

      I suppose the following is exact same thing right?

      int * const start
      int const * start

      both means the pointer is constant - (pointer always point to same
      address)

      Comment

      • Eric Kaplan

        #4
        Re: pointer vs reference

        so start is a reference = memory address?

        content of start is likely to be something like -
        0x12349870h

        ??
        >int const& start
        >
        >start is a reference to a constant int (the int doesn't change).

        Comment

        • Jim Langston

          #5
          Re: pointer vs reference

          Eric Kaplan wrote:
          so start is a reference = memory address?
          >
          content of start is likely to be something like -
          0x12349870h
          >
          ??
          >
          >>int const& start
          >>
          >start is a reference to a constant int (the int doesn't change).
          A reference doesn't actually exist. A reference is an alias. The compiler
          is free to do that however it wishes. The compiler may actually use the
          original variable, or the address, or some other method. You shouldn't
          count on how the compiler does it as it may change from compiler to compiler
          and even from compiler version to version.

          --
          Jim Langston
          tazmaster@rocke tmail.com


          Comment

          • Jim Langston

            #6
            Re: pointer vs reference

            Eric Kaplan wrote:
            I suppose the following is exact same thing right?
            >
            int * const start
            int const * start
            >
            both means the pointer is constant - (pointer always point to same
            address)
            No. Those are not the same.
            const int* start;
            int const* start;
            are exactly the same.

            int* const start;
            start is a constant *pointer* to an int. You can not reseat the poniter.
            You can not change where the pointer is pointing to. In fact you better
            initialize it since you can't change it. But you can change the integer
            that the pointer points to.

            int const * start;
            start is a pointer to a constant *integer* You can not change the value of
            what the pointer points to, but you are free to make the pointer point to
            some other memory location.


            --
            Jim Langston
            tazmaster@rocke tmail.com


            Comment

            • Pete Becker

              #7
              Re: pointer vs reference

              On 2008-04-04 20:20:30 -0400, "Jim Langston" <tazmaster@rock etmail.comsaid:
              >
              >const int * start
              >
              start is a pointer to an int that is constant (the int doesn't change).
              >
              >
              >int const& start
              >
              start is a reference to a constant int (the int doesn't change).
              >
              The value of the int can change, but you can't change it through 'start'.

              int i = 0;
              const int *start = &i;
              std::cout << *start << '\n'; // 0
              ++i; // OK
              std::cout << *start << '\n'; // 1
              ++*start; // illegal

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

              Comment

              Working...