changing reference

W

winstoon

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

Victor Bazarov

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
 
K

Kai-Uwe Bux

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
 
P

Peter

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

BobR

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
 
B

BobR

BobR said:
{
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
 
S

Sarath

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

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?

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;
}
 
J

James Kanze

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

terminator

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top