Reference question in C++ [modified]
-
Hi all, The following question: 1)string str1 = "a"; 2)string str2 = "b"; 3)string& refstr = str1; printf("\n str1: %s,", str1.c_str()); printf("\n str2: %s,", str2.c_str()); printf("\n refstr: %s,\n ", refstr.c_str()); 4)str1 = "aa"; printf("\n str1: %s,", str1.c_str()); printf("\n str2: %s,", str2.c_str()); printf("\n refstr: %s,\n ", refstr.c_str()); 5)refstr = str2; printf("\n str1: %s,", str1.c_str()); printf("\n str2: %s,", str2.c_str()); printf("\n refstr: %s,\n ", refstr.c_str()); 6)str2 = "bb"; printf("\n str1: %s,", str1.c_str()); printf("\n str2: %s,", str2.c_str()); printf("\n refstr: %s,\n ", refstr.c_str()); displays after each stage : 3) str1: a str2: b refstr: a 4) str1: aa str2: b refstr: aa 5) str1: b str2: b refstr: b 6) str1: b, str2: bb, refstr: b, May I get an accurate explantion why this is the display for each stage ? Why after stage 6 the display is not bb for all ? Thanks in advance, Eyal
modified on Sunday, October 11, 2009 7:32 AM
-
Hi all, The following question: 1)string str1 = "a"; 2)string str2 = "b"; 3)string& refstr = str1; printf("\n str1: %s,", str1.c_str()); printf("\n str2: %s,", str2.c_str()); printf("\n refstr: %s,\n ", refstr.c_str()); 4)str1 = "aa"; printf("\n str1: %s,", str1.c_str()); printf("\n str2: %s,", str2.c_str()); printf("\n refstr: %s,\n ", refstr.c_str()); 5)refstr = str2; printf("\n str1: %s,", str1.c_str()); printf("\n str2: %s,", str2.c_str()); printf("\n refstr: %s,\n ", refstr.c_str()); 6)str2 = "bb"; printf("\n str1: %s,", str1.c_str()); printf("\n str2: %s,", str2.c_str()); printf("\n refstr: %s,\n ", refstr.c_str()); displays after each stage : 3) str1: a str2: b refstr: a 4) str1: aa str2: b refstr: aa 5) str1: b str2: b refstr: b 6) str1: b, str2: bb, refstr: b, May I get an accurate explantion why this is the display for each stage ? Why after stage 6 the display is not bb for all ? Thanks in advance, Eyal
modified on Sunday, October 11, 2009 7:32 AM
es1968 wrote:
May I get an accurate explantion why this is the display for each stage ?
Yes, documentation [^] explains it all:
A reference holds the address of an object, but behaves syntactically like an object.
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
es1968 wrote:
May I get an accurate explantion why this is the display for each stage ?
Yes, documentation [^] explains it all:
A reference holds the address of an object, but behaves syntactically like an object.
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi Pallini, Thanks for your the answer, but I want to understand why after stage 6, str1 and strref have not been changed after str2 changed to "bb" ? It's important for me to understand this, probably I miss something. Best, Eyal
es1968 wrote:
why after stage 6, str1 and strref have not been changed after str2 changed to "bb" ?
Because nothing is changing the values of
str1
andstrref
. At stage 6, only the value ofstr2
is changed, so everything else remains the same. What are you not understanding?It is a crappy thing, but it's life -^ Carlo Pallini
-
Hi Pallini, Thanks for your the answer, but I want to understand why after stage 6, str1 and strref have not been changed after str2 changed to "bb" ? It's important for me to understand this, probably I miss something. Best, Eyal
Well, you know, roughly speaking, a reference is a sugar-added-pointer, so let's write your code again, this time using the real thing (BTW no need to say that Klingon developers don't use references, references are for sissies... :rolleyes: )
#include <string>
using namespace std;
#include <stdio.h>
void main()
{
// case 0
string str1 = "a";
string str2 = "b";
string * const pstr = &str1;//that's wath a reference really isprintf("\\ncase 0\\n str1: %s", str1.c\_str()); printf("\\n str2: %s", str2.c\_str()); printf("\\n refstr: %s\\n ", (\*pstr).c\_str()); // case 1 str1 = "aa"; printf("\\n case 1\\n str1: %s", str1.c\_str()); printf("\\n str2: %s", str2.c\_str()); printf("\\n refstr: %s\\n ", (\*pstr).c\_str()); // case 2 (\*pstr) = str2; printf("\\n case 2\\n str1: %s", str1.c\_str()); printf("\\n str2: %s", str2.c\_str()); printf("\\n refstr: %s\\n ", (\*pstr).c\_str()); // case 3 str2 = "bb"; printf("\\n case 3\\n str1: %s", str1.c\_str()); printf("\\n str2: %s", str2.c\_str()); printf("\\n refstr: %s\\n ", (\*pstr).c\_str());
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Well, you know, roughly speaking, a reference is a sugar-added-pointer, so let's write your code again, this time using the real thing (BTW no need to say that Klingon developers don't use references, references are for sissies... :rolleyes: )
#include <string>
using namespace std;
#include <stdio.h>
void main()
{
// case 0
string str1 = "a";
string str2 = "b";
string * const pstr = &str1;//that's wath a reference really isprintf("\\ncase 0\\n str1: %s", str1.c\_str()); printf("\\n str2: %s", str2.c\_str()); printf("\\n refstr: %s\\n ", (\*pstr).c\_str()); // case 1 str1 = "aa"; printf("\\n case 1\\n str1: %s", str1.c\_str()); printf("\\n str2: %s", str2.c\_str()); printf("\\n refstr: %s\\n ", (\*pstr).c\_str()); // case 2 (\*pstr) = str2; printf("\\n case 2\\n str1: %s", str1.c\_str()); printf("\\n str2: %s", str2.c\_str()); printf("\\n refstr: %s\\n ", (\*pstr).c\_str()); // case 3 str2 = "bb"; printf("\\n case 3\\n str1: %s", str1.c\_str()); printf("\\n str2: %s", str2.c\_str()); printf("\\n refstr: %s\\n ", (\*pstr).c\_str());
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
es1968 wrote:
why after stage 6, str1 and strref have not been changed after str2 changed to "bb" ?
Because nothing is changing the values of
str1
andstrref
. At stage 6, only the value ofstr2
is changed, so everything else remains the same. What are you not understanding?It is a crappy thing, but it's life -^ Carlo Pallini