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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. return value of = operator

return value of = operator

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 5 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.
  • D Offline
    D Offline
    Dr Kuulun
    wrote on last edited by
    #1

    Hi, what is the reason for the Cxyz& return value with *this parameter of = operators? Dr-Kuulun

    D I I 3 Replies Last reply
    0
    • D Dr Kuulun

      Hi, what is the reason for the Cxyz& return value with *this parameter of = operators? Dr-Kuulun

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Not sure exactly what you are asking, but this is a pointer while *this is the actual object being pointed to. Since the operator= method is returning a reference to the actual object, that's why *this would need to be returned.


      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

      "There is no death, only a change of worlds." - Native American Proverb

      1 Reply Last reply
      0
      • D Dr Kuulun

        Hi, what is the reason for the Cxyz& return value with *this parameter of = operators? Dr-Kuulun

        I Offline
        I Offline
        includeh10
        wrote on last edited by
        #3

        if u define return type as Cxyz&, u should return *this, otherwise what you can return? u can define return type other way, such as void or BOOL.


        A special image tool for Windows C++ programmers, don't miss it! The world unique Software Label Maker is waiting for you and me ... A nice hyper tool for optimizing your Microsoft html-help contents.

        1 Reply Last reply
        0
        • D Dr Kuulun

          Hi, what is the reason for the Cxyz& return value with *this parameter of = operators? Dr-Kuulun

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          The reason is simpler than you may think. Its is so that you can make multiple assignment work efficiently. You expect this to work:

          int a, b, c;

          a = b = c = 1;

          So you also accept this to work:

          CFwibble a, b, c;

          c.DoSomethingtoInitialisethestruct;
          a = b = c;

          But the last line is equivalent to:

          a = (b = c);

          which means b = c has to be something you can assign to another struct / class / object of the same kind. You could make the return value xyz, but that would mean creating a temporary copy on the stack, which is inefficient. The xyz & means a reference to b is passed, so no temporary copy is made. For proper purity, you should also make the reference const, to prevent b from being messed about with...

          class xyz
          {
          ...
          const xyz &operator=(const xyz &rhs)
          {
          ...
          return *this;
          }
          };

          I hope that made sense for you! Iain. -- modified at 19:54 Monday 3rd April, 2006

          R D 2 Replies Last reply
          0
          • I Iain Clarke Warrior Programmer

            The reason is simpler than you may think. Its is so that you can make multiple assignment work efficiently. You expect this to work:

            int a, b, c;

            a = b = c = 1;

            So you also accept this to work:

            CFwibble a, b, c;

            c.DoSomethingtoInitialisethestruct;
            a = b = c;

            But the last line is equivalent to:

            a = (b = c);

            which means b = c has to be something you can assign to another struct / class / object of the same kind. You could make the return value xyz, but that would mean creating a temporary copy on the stack, which is inefficient. The xyz & means a reference to b is passed, so no temporary copy is made. For proper purity, you should also make the reference const, to prevent b from being messed about with...

            class xyz
            {
            ...
            const xyz &operator=(const xyz &rhs)
            {
            ...
            return *this;
            }
            };

            I hope that made sense for you! Iain. -- modified at 19:54 Monday 3rd April, 2006

            R Offline
            R Offline
            Ryan Binns
            wrote on last edited by
            #5

            Iain Clarke wrote:

            CFwibble a, b, c;

            5 just for the class name! :-D

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            1 Reply Last reply
            0
            • I Iain Clarke Warrior Programmer

              The reason is simpler than you may think. Its is so that you can make multiple assignment work efficiently. You expect this to work:

              int a, b, c;

              a = b = c = 1;

              So you also accept this to work:

              CFwibble a, b, c;

              c.DoSomethingtoInitialisethestruct;
              a = b = c;

              But the last line is equivalent to:

              a = (b = c);

              which means b = c has to be something you can assign to another struct / class / object of the same kind. You could make the return value xyz, but that would mean creating a temporary copy on the stack, which is inefficient. The xyz & means a reference to b is passed, so no temporary copy is made. For proper purity, you should also make the reference const, to prevent b from being messed about with...

              class xyz
              {
              ...
              const xyz &operator=(const xyz &rhs)
              {
              ...
              return *this;
              }
              };

              I hope that made sense for you! Iain. -- modified at 19:54 Monday 3rd April, 2006

              D Offline
              D Offline
              Dr Kuulun
              wrote on last edited by
              #6

              That's it! Thanx! Dr-Kuulun

              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