Returning a class reference
-
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 -
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, JoeTheDelChop wrote:
why does an overloaded operator need to return a reference to the lvalue?
In fact an assignment operator (like
*=
) must return al-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 apointer
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.
-
TheDelChop wrote:
why does an overloaded operator need to return a reference to the lvalue?
In fact an assignment operator (like
*=
) must return al-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 apointer
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.
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 thethis
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
-
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, JoeWhen you declare a variable the
&
does not mean "address of" but rather "reference". ieint 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
-
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 thethis
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
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.
-
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.
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
-
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, JoeTheDelChop wrote:
why does an overloaded operator need to return a reference to the lvalue?
So you can write chained assignments like
x = y = z;
Ify=z
doesn't return a reference toy
, then the second assignment may not compile, or may call some otheroperator=
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?
-
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;
Ify=z
doesn't return a reference toy
, then the second assignment may not compile, or may call some otheroperator=
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?
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.