473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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?

Jul 17 '07 #1
10 1504
wi******@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
Jul 17 '07 #2
wi******@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
Jul 17 '07 #3
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.

Jul 17 '07 #4

<wi******@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<<"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
Jul 17 '07 #5

BobR <re***********@worldnet.att.netwrote 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
Jul 17 '07 #6
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
http://www.parashift.com/c++-faq-lit...s.html#faq-8.2

Regards,
Sarath
http://sarathc.wordpress.com/

Jul 17 '07 #7
You can not, use pointer instead.

Jul 17 '07 #8
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;
}

Jul 18 '07 #9
On Jul 17, 4:14 am, "BobR" <removeBadB...@worldnet.att.netwrote:
<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<<"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:ja*********@gmail.com
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

Jul 18 '07 #10
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.

Jul 18 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pablo | last post by:
Dear NGers, I want to keep some images just left of some DIVs. But a change in text size leaves my images at the original position. Which event is generated by changing the text size from the...
14
by: Brandon Hoppe | last post by:
I'm trying to change the src of an ilayer in the parent document from a link inside the ilayer. I'm not able to get it to work. All that happens is Netscape 4 crashes. This is for Netscape 4 only....
2
by: Zippy | last post by:
Some months ago, we requested help from this newsgroup on how to replace the library reference of a database with another library reference, prior to creating an MDE. I got the following answer...
6
by: Eric | last post by:
I have an array, result. I populate the array and add it to an ArrayList. I then change result and add the new version to the ArrayList. However, when I go to review the ArrayList, all of the...
10
by: Daniel | last post by:
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?
5
by: Bob P | last post by:
I have an example below that is not working correctly. I have a web application that passes a System.Xml.XmlDocument object "oInputDoc" around to functions byval. I do not want the "oInputDoc"...
2
by: Tommy Martin | last post by:
If I add a web reference to my code it works fine. I need to know how to dynamically set the url for the web reference. Sometimes URL's change and would be nice to let the user enter a new URL in a...
1
by: Nick via .NET 247 | last post by:
I have a similar problem trying to use web services with awindows form application. I currently use a static web referencein my project. What I am trying to do is to programaticallychange the url...
5
by: marton | last post by:
Hi there, I'm new to posting on the forum, and I've been working with MS Access for a couple of years, but and other than that know nothing about programming. I have a fairly simple, networked,...
1
by: DrJarmin | last post by:
Hello The problem is this: in the criteria for a list box I reference the parent form - and Access KEEPS changing the criteria for one that won't work. Details below: I have a couple of list...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.