changing reference

Discussion in 'C++' started by winstoon@gmail.com, Jul 17, 2007.

  1. Guest

    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?
     
    , Jul 17, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jul 17, 2007
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jul 17, 2007
    #3
  4. Peter Guest

    On Jul 17, 8:14 am, 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.
     
    Peter, Jul 17, 2007
    #4
  5. BobR Guest

    <> wrote 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<<"string &c(a) c="<<c<<std::endl;
    a.swap( b );
    std::cout<<"a.swap(b) c="<<c<<std::endl;
    }
    // out: string &c(a) c=1
    // out: a.swap(b) c=2

    --
    Bob R
    POVrookie
     
    BobR, Jul 17, 2007
    #5
  6. BobR Guest

    BobR <> wrote in message...
    > {
    > std::string a = "1";
    > std::string b = "2";
    > std::string &c( a );
    > std::cout<<"string &c(a) c="<<c<<std::endl;
    > a.swap( b );
    > std::cout<<"a.swap(b) c="<<c<<std::endl;
    > }
    > // 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<<"string &c(a) c="<<c<<std::endl;
    }
    {
    std::string &c( b );
    std::cout<<"string &c(b) c="<<c<<std::endl;
    }
    }
    // out: string &c(a) c=1
    // out: string &c(b) c=2

    --
    Bob R
    POVrookie
     
    BobR, Jul 17, 2007
    #6
  7. Sarath Guest

    On Jul 17, 9:14 am, 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
    http://www.parashift.com/c -faq-lite/references.html#faq-8.2

    Regards,
    Sarath
    http://sarathc.wordpress.com/
     
    Sarath, Jul 17, 2007
    #7
  8. sun1991 Guest

    You can not, use pointer instead.
     
    sun1991, Jul 17, 2007
    #8
  9. Guest Guest

    On Jul 17, 8:14 am, 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;
    }
     
    Guest, Jul 18, 2007
    #9
  10. James Kanze Guest

    On Jul 17, 4:14 am, "BobR" <> wrote:
    > <> wrote 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<<"string &c(a) c="<<c<<std::endl;
    > a.swap( b );
    > std::cout<<"a.swap(b) c="<<c<<std::endl;}


    > // 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::boolalpha ) ;
    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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 18, 2007
    #10
  11. terminator Guest

    On Jul 17, 4:14 am, 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.
     
    terminator, Jul 18, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samridhi Kumar Shukla
    Replies:
    1
    Views:
    540
    Alvin Bruney
    Nov 30, 2003
  2. =?Utf-8?B?VmlkZHM=?=

    Changing the Webservice reference dynamically

    =?Utf-8?B?VmlkZHM=?=, Jan 3, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,416
    =?Utf-8?B?VmlkZHM=?=
    Jan 4, 2006
  3. mosscliffe
    Replies:
    3
    Views:
    919
    Cowboy \(Gregory A. Beamer\)
    Jun 24, 2006
  4. Aaron Beall
    Replies:
    2
    Views:
    1,915
    Aaron Beall
    Sep 14, 2007
  5. VK
    Replies:
    3
    Views:
    202
    Dr J R Stockton
    Mar 15, 2007
Loading...

Share This Page