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. Returning a class reference

Returning a class reference

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
8 Posts 4 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.
  • T Offline
    T Offline
    TheDelChop
    wrote on last edited by
    #1

    Question about the 'this' pointer and properly returning a reference to an object. I am learning about OOP and working through some examples. One of the examples has me overloading the *= operator. CRegister& CRegister::operator *=(const CRegister &rhs) { this->m_dStore *= rhs.m_dStore; return *this; } I have two questions, why does an overloaded operator need to return a reference to the lvalue? Second if I did want to return the address of the lvalue shouldn't the code look like this? CRegister& CRegister::operator *=(const CRegister &rhs) { this->m_dStore *= rhs.m_dStore; return &this; } Or because 'this' is a pointer, by returning '&this' am I creating a second level of indirection, because i don't want the address of the 'this' pointer, but rather the address that the 'this' pointer points to? Thanks guys, Joe

    C S M 3 Replies Last reply
    0
    • T TheDelChop

      Question about the 'this' pointer and properly returning a reference to an object. I am learning about OOP and working through some examples. One of the examples has me overloading the *= operator. CRegister& CRegister::operator *=(const CRegister &rhs) { this->m_dStore *= rhs.m_dStore; return *this; } I have two questions, why does an overloaded operator need to return a reference to the lvalue? Second if I did want to return the address of the lvalue shouldn't the code look like this? CRegister& CRegister::operator *=(const CRegister &rhs) { this->m_dStore *= rhs.m_dStore; return &this; } Or because 'this' is a pointer, by returning '&this' am I creating a second level of indirection, because i don't want the address of the 'this' pointer, but rather the address that the 'this' pointer points to? Thanks guys, Joe

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

      TheDelChop wrote:

      why does an overloaded operator need to return a reference to the lvalue?

      In fact an assignment operator (like *=) must return a l-value.

      TheDelChop wrote:

      Or because 'this' is a pointer, by returning '&this' am I creating a second level of indirection, because i don't want the address of the 'this' pointer, but rather the address that the 'this' pointer points to?

      Your guess is right. this is a pointer to the object itself, *this is a reference to the object itself. :)

      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.

      S 1 Reply Last reply
      0
      • C CPallini

        TheDelChop wrote:

        why does an overloaded operator need to return a reference to the lvalue?

        In fact an assignment operator (like *=) must return a l-value.

        TheDelChop wrote:

        Or because 'this' is a pointer, by returning '&this' am I creating a second level of indirection, because i don't want the address of the 'this' pointer, but rather the address that the 'this' pointer points to?

        Your guess is right. this is a pointer to the object itself, *this is a reference to the object itself. :)

        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.

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        CPallini wrote:

        TheDelChop wrote:

        why does an overloaded operator need to return a reference to the lvalue?

        In fact an assignment operator (like *=) must return a l-value.

        No, it doesn't. It can return anything or nothing. The only requirement is that it takes two arguments: if it's a global function (usually a friend function) two explicit arguments, at least one being a user defined type; if it's a member function the this pointer is the first implicit argument and the second must be explicitly supplied. In most common usages the return type is a reference to the class to which the operator belongs, but this is not a requirement.

        Steve

        C 1 Reply Last reply
        0
        • T TheDelChop

          Question about the 'this' pointer and properly returning a reference to an object. I am learning about OOP and working through some examples. One of the examples has me overloading the *= operator. CRegister& CRegister::operator *=(const CRegister &rhs) { this->m_dStore *= rhs.m_dStore; return *this; } I have two questions, why does an overloaded operator need to return a reference to the lvalue? Second if I did want to return the address of the lvalue shouldn't the code look like this? CRegister& CRegister::operator *=(const CRegister &rhs) { this->m_dStore *= rhs.m_dStore; return &this; } Or because 'this' is a pointer, by returning '&this' am I creating a second level of indirection, because i don't want the address of the 'this' pointer, but rather the address that the 'this' pointer points to? Thanks guys, Joe

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          When you declare a variable the & does not mean "address of" but rather "reference". ie

          int i = 1;
          int &ri = i; // 'ri' is a reference to 'i'.
          ri = 2; // 'i' is now 2.
          int *pInt = &i; // 'pInt' is a pointer to 'i'. Here & means "address of".
          int &r2 = *pInt; // 'r2' is a reference to 'i'. Here the * dereferences the pointer. You can think of this as turning the pointer into a reference.
          r2 = 3; // 'i' is now 3.

          Steve

          1 Reply Last reply
          0
          • S Stephen Hewitt

            CPallini wrote:

            TheDelChop wrote:

            why does an overloaded operator need to return a reference to the lvalue?

            In fact an assignment operator (like *=) must return a l-value.

            No, it doesn't. It can return anything or nothing. The only requirement is that it takes two arguments: if it's a global function (usually a friend function) two explicit arguments, at least one being a user defined type; if it's a member function the this pointer is the first implicit argument and the second must be explicitly supplied. In most common usages the return type is a reference to the class to which the operator belongs, but this is not a requirement.

            Steve

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

            Stephen Hewitt wrote:

            CPallini wrote: TheDelChop wrote: why does an overloaded operator need to return a reference to the lvalue? In fact an assignment operator (like *=) must return a l-value. No, it doesn't.

            At least, it should (MSDN on assignment operator): The operator returns the object to preserve the behavior of the assignment operator,which returns the value of the left side after the assignment is complete. This allows writing statements such as: pt1 = pt2 = pt3; :)

            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.

            S 1 Reply Last reply
            0
            • C CPallini

              Stephen Hewitt wrote:

              CPallini wrote: TheDelChop wrote: why does an overloaded operator need to return a reference to the lvalue? In fact an assignment operator (like *=) must return a l-value. No, it doesn't.

              At least, it should (MSDN on assignment operator): The operator returns the object to preserve the behavior of the assignment operator,which returns the value of the left side after the assignment is complete. This allows writing statements such as: pt1 = pt2 = pt3; :)

              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.

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              CPallini wrote:

              The operator returns the object to preserve the behavior of the assignment operator,which returns the value of the left side after the assignment is complete. This allows writing statements such as: pt1 = pt2 = pt3;

              As I said – this is normally the case (returning a reference) – but not a requirement as you suggested.

              Steve

              1 Reply Last reply
              0
              • T TheDelChop

                Question about the 'this' pointer and properly returning a reference to an object. I am learning about OOP and working through some examples. One of the examples has me overloading the *= operator. CRegister& CRegister::operator *=(const CRegister &rhs) { this->m_dStore *= rhs.m_dStore; return *this; } I have two questions, why does an overloaded operator need to return a reference to the lvalue? Second if I did want to return the address of the lvalue shouldn't the code look like this? CRegister& CRegister::operator *=(const CRegister &rhs) { this->m_dStore *= rhs.m_dStore; return &this; } Or because 'this' is a pointer, by returning '&this' am I creating a second level of indirection, because i don't want the address of the 'this' pointer, but rather the address that the 'this' pointer points to? Thanks guys, Joe

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                TheDelChop wrote:

                why does an overloaded operator need to return a reference to the lvalue?

                So you can write chained assignments like x = y = z; If y=z doesn't return a reference to y, then the second assignment may not compile, or may call some other operator= that you weren't expecting.

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                T 1 Reply Last reply
                0
                • M Michael Dunn

                  TheDelChop wrote:

                  why does an overloaded operator need to return a reference to the lvalue?

                  So you can write chained assignments like x = y = z; If y=z doesn't return a reference to y, then the second assignment may not compile, or may call some other operator= that you weren't expecting.

                  --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                  T Offline
                  T Offline
                  TheDelChop
                  wrote on last edited by
                  #8

                  Michael Dunn wrote:

                  So you can write chained assignments like x = y = z; If y=z doesn't return a reference to y, then the second assignment may not compile, or may call some other operator= that you weren't expecting.

                  Nice, this is exactly the explanation I was looking for. Thank you very much Mike.

                  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