Address of a reference

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

    #1

    Address of a reference

    Is this ok?

    #include <iostream>

    int main(int argc, char *argv[])
    {
    int x=1234;
    int *x_ptr=&x;
    int &x_ref=x;

    std::cout << std::hex << x_ptr << "=" << *x_ptr << std::endl;
    int *y=&x_ref;
    std::cout << std::hex << y << "=" << *y << std::endl;

    return 0;
    }


    Cant find anything in my standard about taking addresses of
    references.

    Adrian

  • Adrian

    #2
    Re: Address of a reference

    I should have added, that I actually want a pointer to the original
    object but only have a reference.

    Comment

    • Gianni Mariani

      #3
      Re: Address of a reference

      Adrian wrote:
      Is this ok?
      >
      #include <iostream>
      >
      int main(int argc, char *argv[])
      {
      int x=1234;
      int *x_ptr=&x;
      int &x_ref=x;
      >
      std::cout << std::hex << x_ptr << "=" << *x_ptr << std::endl;
      int *y=&x_ref;
      std::cout << std::hex << y << "=" << *y << std::endl;
      >
      return 0;
      }
      >
      >
      Cant find anything in my standard about taking addresses of
      references.
      You can't take the address of a reference. A reference is like an alias
      for the object it is referencing.

      You can "contain" a reference by placing it in a class; e.g.

      template <typename Tstruct Ref
      {
      T & v;
      Ref( T & iv ) : v( iv ) {}
      };

      int main()
      {
      int x=1234;
      Ref<int refx( x );
      }

      however, it's somewhat useless because you can't reseat a reference
      after it has been initialized (unlike a pointer).

      The truly nasty hack you can to to reseat a reference is to reconstruct
      the class in place (as I said - a major bad).


      int main()
      {
      int x=1234;
      Ref<int refx( x );

      int y=444;

      refx.~Ref<int>( );

      new (&refx) Ref<int>( y );
      }

      (I'm not sure it's compilable but you get the idea)

      It is perfectly legitimate C++ code but I think you will be ridiculed if
      you use it like I will be for pointing it out :-)....


      Comment

      • Rolf Magnus

        #4
        Re: Address of a reference

        Adrian wrote:
        Is this ok?
        >
        #include <iostream>
        >
        int main(int argc, char *argv[])
        {
        int x=1234;
        int *x_ptr=&x;
        int &x_ref=x;
        >
        std::cout << std::hex << x_ptr << "=" << *x_ptr << std::endl;
        int *y=&x_ref;
        std::cout << std::hex << y << "=" << *y << std::endl;
        >
        return 0;
        }
        >
        >
        Cant find anything in my standard about taking addresses of
        references.
        That's because references don't have addresses.
        In your code, y will just contain the adress of the object that the
        reference refers to.

        Comment

        • Gianni Mariani

          #5
          Re: Address of a reference

          Adrian wrote:
          I should have added, that I actually want a pointer to the original
          object but only have a reference.
          >
          Taking the address of a reference will give you the address of the
          object the reference is referring to.

          Comment

          • Adrian

            #6
            Re: Address of a reference

            On Mar 26, 4:28 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
            Adrian wrote:
            I should have added, that I actually want a pointer to the original
            object but only have a reference.
            >
            Taking the address of a reference will give you the address of the
            object the reference is referring to.
            Thanks, that what I wanted and thats what my example does - but wanted
            to make sure that is what it is supposed to do :-)


            Comment

            Working...