changing reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • winstoon@gmail.com

    #1

    changing reference

    Hi,

    consider the following code...

    string a = "1";
    string b = "2";
    string& c = a;

    how do I change c to refer to b instead of a?

  • Victor Bazarov

    #2
    Re: changing reference

    winstoon@gmail. com wrote:
    consider the following code...
    >
    string a = "1";
    string b = "2";
    string& c = a;
    >
    how do I change c to refer to b instead of a?
    There is no legal C++ way.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Kai-Uwe Bux

      #3
      Re: changing reference

      winstoon@gmail. com wrote:
      consider the following code...
      >
      string a = "1";
      string b = "2";
      string& c = a;
      >
      how do I change c to refer to b instead of a?
      There is no "how": you cannot reseat a reference.


      Best

      Kai-Uwe Bux

      Comment

      • Peter

        #4
        Re: changing reference

        On Jul 17, 8:14 am, winst...@gmail. com wrote:
        Hi,
        >
        consider the following code...
        >
        string a = "1";
        string b = "2";
        string& c = a;
        >
        how do I change c to refer to b instead of a?
        When a reference is initialized, it remains bound to that object as
        long as the reference exists. There is no way to rebind a reference to
        a different object.

        Comment

        • BobR

          #5
          Re: changing reference


          <winstoon@gmail .comwrote in message...
          Hi,
          consider the following code...
          string a = "1";
          string b = "2";
          string& c = a;
          how do I change c to refer to b instead of a?
          >
          Can't reseat 'c'.

          {
          std::string a = "1";
          std::string b = "2";
          std::string &c( a );
          std::cout<<"str ing &c(a) c="<<c<<std::en dl;
          a.swap( b );
          std::cout<<"a.s wap(b) c="<<c<<std::en dl;
          }
          // out: string &c(a) c=1
          // out: a.swap(b) c=2

          --
          Bob R
          POVrookie


          Comment

          • BobR

            #6
            Re: changing reference


            BobR <removeBadBobR@ worldnet.att.ne twrote in message...
            {
            std::string a = "1";
            std::string b = "2";
            std::string &c( a );
            std::cout<<"str ing &c(a) c="<<c<<std::en dl;
            a.swap( b );
            std::cout<<"a.s wap(b) c="<<c<<std::en dl;
            }
            // out: string &c(a) c=1
            // out: a.swap(b) c=2
            or, scope it:

            {
            std::string a = "1";
            std::string b = "2";
            {
            std::string &c( a );
            std::cout<<"str ing &c(a) c="<<c<<std::en dl;
            }
            {
            std::string &c( b );
            std::cout<<"str ing &c(b) c="<<c<<std::en dl;
            }
            }
            // out: string &c(a) c=1
            // out: string &c(b) c=2

            --
            Bob R
            POVrookie


            Comment

            • Sarath

              #7
              Re: changing reference

              On Jul 17, 9:14 am, winst...@gmail. com wrote:
              Hi,
              >
              consider the following code...
              >
              string a = "1";
              string b = "2";
              string& c = a;
              >
              how do I change c to refer to b instead of a?
              It's not possible to change the reference. Reference is not a pointer
              to the object. It's simply the same object. Something we can call
              alias. So assignment after initializing reference is something like
              you assign a value to the object it is referring.

              See


              Regards,
              Sarath


              Comment

              • sun1991

                #8
                Re: changing reference

                You can not, use pointer instead.

                Comment

                • Guest's Avatar

                  #9
                  Re: changing reference

                  On Jul 17, 8:14 am, winst...@gmail. com wrote:
                  Hi,
                  >
                  consider the following code...
                  >
                  string a = "1";
                  string b = "2";
                  string& c = a;
                  >
                  how do I change c to refer to b instead of a?
                  So called "reference" is the alias of the object it refers to. The
                  referer and referee have the same physical address, and the referer's
                  address is set at the time of initialization, it can not be changed
                  anyway. Consider the example below:

                  nt main()
                  {
                  int a = 1 ;
                  int b = 2;
                  int& c = a;

                  cout << "c is " << c << endl;

                  c = b;
                  cout << "Now a is:" << a << endl;

                  c = 3;
                  cout << "Now b still is:" << b << endl;

                  cout << "But now a is:" << a << endl;

                  cout << "c's address:" << &c << endl;
                  cout << "a's address:" << &a << endl;

                  return 0;
                  }

                  Comment

                  • James Kanze

                    #10
                    Re: changing reference

                    On Jul 17, 4:14 am, "BobR" <removeBadB...@ worldnet.att.ne twrote:
                    <winst...@gmail .comwrote in message...
                    consider the following code...
                    string a = "1";
                    string b = "2";
                    string& c = a;
                    how do I change c to refer to b instead of a?
                    Can't reseat 'c'.
                    {
                    std::string a = "1";
                    std::string b = "2";
                    std::string &c( a );
                    std::cout<<"str ing &c(a) c="<<c<<std::en dl;
                    a.swap( b );
                    std::cout<<"a.s wap(b) c="<<c<<std::en dl;}
                    // out: string &c(a) c=1
                    // out: a.swap(b) c=2
                    I'm not sure what this is supposed to show. Consider:

                    std::cout.setf( std::ios::boola lpha ) ;
                    std::string a = "1" ;
                    std::string b = "2" ;
                    std::cout << "&a = " << &a << ", &b = " << &b << std::endl ;
                    std::string& c = a ;
                    std::cout << "a = " << a
                    << ", b = " << b
                    << ", c = " << c
                    << ", &c = " << &c << std::endl ;
                    a.swap( b ) ;
                    std::cout << "a = " << a
                    << ", b = " << b
                    << ", c = " << c
                    << ", &c = " << &c << std::endl ;

                    I get:
                    &a = ffbedfe0, &b = ffbedfd8
                    a = 1, b = 2, c = 1, &c = ffbedfe0
                    a = 2, b = 1, c = 2, &c = ffbedfe0
                    You've swapped a and b, but you've not changed the binding of c
                    in any way. Once constructed, there is no legal way to change
                    the binding of a reference.

                    --
                    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

                    • terminator

                      #11
                      Re: changing reference

                      On Jul 17, 4:14 am, winst...@gmail. com wrote:
                      Hi,
                      >
                      consider the following code...
                      >
                      string a = "1";
                      string b = "2";
                      string& c = a;
                      >
                      how do I change c to refer to b instead of a?
                      Sorry, you can`t do that ,but I suggest using pointers instead:

                      string a = "a";
                      string b = "b";
                      string d = "d";
                      string e = "e";

                      string * sptr = & a;
                      ( * sptr ) = "a is changed";

                      sptr = & b;
                      ( * sptr ) = "b is changed";

                      ( * ( sptr = & d ) ) = "d is changed";

                      ( *( & e ) ) = "e is changed";

                      regards,
                      FM.

                      Comment

                      Working...