Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reference question in C++ [modified]

Reference question in C++ [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    es1968
    wrote on last edited by
    #1

    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

    CPalliniC 1 Reply Last reply
    0
    • E es1968

      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

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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]

      In testa che avete, signor di Ceprano?

      E 1 Reply Last reply
      0
      • CPalliniC CPallini

        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]

        E Offline
        E Offline
        es1968
        wrote on last edited by
        #3

        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

        R CPalliniC 2 Replies Last reply
        0
        • E es1968

          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

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          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 and strref. At stage 6, only the value of str2 is changed, so everything else remains the same. What are you not understanding?

          It is a crappy thing, but it's life -^ Carlo Pallini

          E 1 Reply Last reply
          0
          • E es1968

            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

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            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 is

            printf("\\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]

            In testa che avete, signor di Ceprano?

            E 1 Reply Last reply
            0
            • CPalliniC CPallini

              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 is

              printf("\\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]

              E Offline
              E Offline
              es1968
              wrote on last edited by
              #6

              CPallini, Thanks a lot, Eyal

              1 Reply Last reply
              0
              • R Rajesh R Subramanian

                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 and strref. At stage 6, only the value of str2 is changed, so everything else remains the same. What are you not understanding?

                It is a crappy thing, but it's life -^ Carlo Pallini

                E Offline
                E Offline
                es1968
                wrote on last edited by
                #7

                Rajesh, Thanks a lot, Eyal

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups