I have the following code to overload operator+, which works fine when the program gets executed.
In the main() function, when I re-write the statement to call the overloaded operator+ from res= t + t1 + t2, which works fine, to res = t + (t1 + t2), it does not work anymore. Can anyone provide me a solution, along with the reason?
A solution already found is to update the signatures of the operator+ from Test operator +(Test &a) to Test operator +(const Test &a). Here, I have used the const keyword in the parameter list.
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(int v)
{
num = v;
}
Test operator+(Test &a)
{
Test r(0);
r = num + a.num;
return r;
}
void show()
{
cout << "\n num = " << num;
}
};
int main()
{
Test t(10);
Test t1(20);
Test t2(60);
Test res(0);
res = t + t1 + t2;
res.show();
return 0;
}
The problem is that you are accepting an object by non-const reference, not by const reference.
The Test object returned by operator+() is a temporary. A non-const reference can't bind to a temporary.
The reason this was probably working before is the operator+ was executed from left to right - which would look something like this:
object + object + object
temporary + object
The temporary still has the function operator+(), so it can still be called.
On the other hand, when you use parenthesis, it executes like this:
object + object + object
object + temporary
That means that the temporary ends up in a, which, again, can't happen for the reasons stated above.
To fix, either a) turn it into a const reference, or b) pass by value (not recommended because it creates extra copies in memory you don't need):
// a
Test operator +(const Test &a)
// b
Test operator +(Test a)
I also highly recommend making this function a const as well:
// a
Test operator +(const Test &a) const
// b
Test operator +(Test a) const
Now you can add const objects even if they are on the right hand side as well.
(t1 + t2) evaluates to an rvalue expression.
But your operator+(&) can only bind to non-const lvalues. On the other hand const & can bind to both lvalues and rvalues, thus operator+(const &) works.
Related
I am trying to override operator+ and operator= for my classes.
This is my code:
#include <iostream>
#include <vector>
using namespace std;
class Integer {
public:
int i;
Integer operator+ (Integer&);
Integer& operator= (Integer&);
};
Integer Integer::operator+(Integer& rhs) {
Integer res;
res.i = this->i + rhs.i;
return res;
}
Integer& Integer::operator=(Integer& rhs) {
this->i = rhs.i;
return *this;
}
int main()
{
Integer i1, i2, i3;
i1.i = 1, i2.i = 2;
i3 = i1 + i2;
cout << i3.i;
}
In visual studio 2017, the compiler complained that:
"E0349 no operator "=" matches these operands"
It seems that an Integer object doesn't match with Integer& in the operator= function. But it works for operator+ function. It is very confusing.
Thanks a lot.
i3 = i1 + i2;
returns a temporary variable of type Integer.
Now, your operator= takes a Integer&. This is a reference to an Integer. The problem is that temporaries cannot bind to non-const references.
Just make sure you change to const Integer&. That is the default for all operator overloads anyway.
I'll break this down from the assignment statement.
i3 = i1 + i2;
The first thing that happens were is the addition expression gets evaluated.
That's this code snippet:
i1 + i2
You designed your operator overload to handle this expression.
Your custom addition operator overload returns a new Integer instance.
But the problem occurs when you try to assign this new instance to the parameter
in your assignment overload. Here's a snippet of that code.
Integer& Integer::operator=(Integer& rhs);
When you use a referenced parameter in a function declaration, the compiler checks to make sure that this value is not a temporary value, so that it will exist after the function is called. The compiler has determined (correctly) that the argument passed to this assignment statement is a temporary value. That is why it spits out an error when you try to do this.
The most logical why I see of making your code make sense is to implement the following:
Integer& Integer::operator=(const Integer& rhs) {
this->i = rhs.i;
return *this;
}
By adding the const-specifier, you tell the compiler that you're not relying on rhs to persist existence after calling the function. In that case, the value passed to the parameter may be a temporary value.
I read it from 'Data Structures and Algorithm Analysis in C++'. The return type of the member function operator= is const Vector&. I wonder if the vector can be modified later?
C++ Primer says we can not change a const reference, it gives this example:
const string& shorterString(const string& s1 , const string& s2)
shorterString("hi","bye") = "X" //wrong
Usually operator= returns a non-const reference to the caller. You can see this in classes from STL, for example.
However, the return type rarely matters, because to return something from assignment is an additional effect which, for example, allows chaining:
a = (b = (c = d));
(a = b).callMethod();
operator= could be void as well, and assignment expressions couldn't be used somewhere else. But they would still work, modify its argument and leave it modifiable, i.e. non-const:
A a, b, c;
b = c; //call, say, void A::operator=(const A &)
a = b; //ditto
//a = b = c; //impossible here
a = c; //works
So, the modification of a has nothing to do with the return type of the assignment operator, but with its side-effects.
As for your second example, shorterString which gets two const-references and returns one of them, should obviously leave it const. And if you need to work with the result in a non-const way, you must use non-const arguments as well: string& shorterString(string& s1, string& s2). These two functions can be overloads, and the compiler will choose an appropriate one.
I was going through reference return and came across temporary objects. I don't understand how to identify them. Please explain using this example:
If a and b are objects of same class, consider binary operator+. If you use it in an expression such as f(a+b), then a+b becomes temporary object and f has to of form f(const <class name>&) or f(<class name>). It can't be of form f(<class name>&) However, (a+b).g() is perfectly alright where g() can even change contents of object returned by a+b.
When you say f(a + b), the parameter of f needs to bind to the value with which the function was called, and since that value is an rvalue (being the value of a function call with non-reference return type)*, the parameter type must be a const-lvalue-reference, an rvalue-reference or a non-reference.
By constrast, when you say (a + b).g(), the temporary object is used as the implicit instance argument in the member function call, which does not care about the value category. Mutable values bind to non-const and const member functions, and const values only bind to const member functions (and similarly for volatile).
Actually, C++11 did add a way to qualify the value category of the implicit instance argument, like so:
struct Foo()
{
Foo operator+(Foo const & lhs, Foo const & rhs);
void g() &; // #1, instance must be an lvalue
void g() &&; // #2, instance must be an rvalue
}
Foo a, b;
a.g(); // calls #1
b.g(); // calls #1
(a + b).g(); // calls #2
*) this is the case for an overloaded operator as in this example, and also for built-in binary operators. You can of course make overloaded operators which produce lvalues, though going against the common conventions would probably be considered very confusing.
Your confusion comes not from that you cannot identify temporary objects, in both cases result of a+b is temporary object, but wrong assumption that non const method requires lvalue and would not accept temporary object, which is not true.
For a simple case, think of following piece of code:
int func(int lhs, int rhs)
{
return lhs + rhs;
}
int main() {
int a = 1, b = 2, c = 3;
return func(a * c, b * c);
}
Because func takes two integers, the program must calculate the values of a * c and b * c and store them somewhere -- it can't store them in a or b or c. So the resulting code is equivalent to:
int lhsParam = a * c;
int rhsParam = b * c;
return func(lhsParam, rhsParam);
Again, at the end of func() we return a calculate value, lhs + rhs. The compiler must store it in a new place.
For integers and so forth this seems very simple, but consider instead
int function(std::string filename);
function("hello");
filename has to be a std::string, but you passed a const char*. So what the compiler does is:
std::string filenameParam = "hello"; // construct a new object
function(filenameParam);
just like the previous example, but this time it is hopefully clearer that we're constructing a temporary object.
Note: The convention of calling them "somethingParam" is just for clarity in this answer.
This is a follow up question from Calling constructor in return statement.
This a operator overload fun in a class.
const Integer operator+(const Integer& IntObject)
{
cout << "Data : " << this->data << endl;
return Integer(this->data + IntObject.data);
}
What is the relevance of const in the return type for such functions?
int main()
{
Integer A(1); //Create 2 object of class Integer
Integer B(2);
const Integer C = A + B; //This will work
Integer D = A + B; //This will also work
fun(A + B); //Will work
}
void fun(Integer F) {}
This is a case temporaries are not created during return step due to NRVO. The object to be returned is directly constructed on the callee's address.
Here's a better example:
struct Foo
{
void gizmo();
Foo const operator+(Foo const & rhs);
};
Now if you have a Foo x; Foo y;, then you cannot say:
(x + y).gizmo(); // error!
The constant return value means you cannot use it for non-constant operations. For primitive types this is not quite so relevant, because there aren't many non-constant operations you can perform on temporary objects, because lots of "interesting" operations (like prefix-++) aren't allowed on temporaries.
That said, with C++11 one should really try and adopt the new idiom of never returning constant values, since non-constant values are now amenable to move optimisations.
Some people used to suggest doing that, to prevent writing nonsense like A + B = C. However, in C++11 it can prevent some optimisations since it makes the return value unmovable. Therefore, you shouldn't do it.
In this case, it also prevents you from writing perfectly valid code like D = A + B + C, but that's just because the author forgot to declare the operator const.
There is no relevance in your code snippet, because you are making a copy of the returned value.
In general, it is difficult to find good reasons to return a const value. I can only see it having an effect in this type of expression, attempting to call a non-const method on a const temporary:
(someObject.someMethodReturningConstValue()).someNonConstMethod(); // error, calls non const method on const temporary
so you should only use it if you want to disallow calling non-const methods on temporaries. On the other hand, it kills move-semantics in C++11 so is discouraged.
In my C++ class they were discussing how to create an assignment operator. At the end of the assignment was the line "return *this," which they said returned a reference to the object that "this" points to. Why does it return a reference? If "this" is being dereferenced, shouldn't it just return the object?
A function returns a reference if its declaration (i.e. its signature) tells so.
So (assuming a class Foo) if a function is declarated
Foo fun();
then it returns a value (with copying, etc..)
But if it is declared as
Foo& fun();
or as
const Foo& fun();
a reference is returned.
The statement return *this; don't define by itself if a reference or a value is returned.
It returns the current instance of the type MyClass you are in. It's returned as reference because the assignment operator was explicitly told to return a reference.
MyClass& operator = (MyClass& other) { return *this; }
Note the & after MyClass as the return value. A reference is returned.
Unless the & weren't there right before operator, the instance would be returned by value.
The expression *this
At the end of the assignment was the line "return *this," which they said returned a reference to the object that "this" points to
They were wrong.
Why does it return a reference?
It doesn't.
If "this" is being dereferenced, shouldn't it just return the object?
Yes!
Dereferencing a pointer yields an lvalue. That means that the result of *this is the actual object that this points to. It's not a reference, but it's not a copy either.
[C++11: 5.3.1/1]: The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.” [..]
It can be hard to conceptualise, since you can never do this yourself in code; it's just a feature of the * operator for native pointers, and has been since C.
Returning from operator=
Because you can't do it yourself, conventionally you'd bind that lvalue expression to a reference in order to use it in different contexts. For example, the expression *this in return *this gets bound to the return type of the function that you're returning from; in this case, an assignment operator.
Now, we could have the assignment operator return by value in which case an object copy would be made from the lvalue that comes from *this; however, for an assignment operator we usually return by reference so that we avoid an almost-certainly needless copy, and can perform chaining:
Type a, b, c;
c = b = a;
It's a convention with benefits, and no downsides. Can you think of a situation when you'd want op= to return by value?
Every dereferenced pointer is a reference to its pointee, else you'd 'loose' the pointee you're pointing to.
Invoke method twice on the same object, using a pointer and a reference:
MyClass* objPtr = .... ;
objPtr->doSomething();
MyClass& objRef = *objPtr;
objRef.doSomething();
Invoke method on distinct objects; original and copy:
MyClass* objPtr = .... ;
objPtr->doSomething();
MyClass objCopy = *objPtr; //here, the reference is passed to the (implicit or implemented) copy constructor if possible, else a compile time error occurs.
objCopy.doSomething();
That means, if you return a reference from an operator method which has MyClass (rvalue) instead of MyClass& (lvalue) as return type, a copy of *this (MyClass&) is created by reference (leaving aside return value optimizations and rvalue references).
This is useful for non modifying const methods such as + and - which have a new value as result while leaving the object on which this method was invoked unmodified.
Operators like += and your assignment operator modify the object inplace by convention and should therefore return a reference to allow expressions like primitive types support it, since a temporary copy may vanish and cause unexpected results:
Consider this expression:
int i = 4;
int r = (i += 3) <<= 2;
The result r is 28 (added and shifted inplace).
What is the value of i? 28 too, what else.
But what if hypothetically int::operator+= would return a copy of itself instead of a reference to itself?
The result r would be 28 too.
But the value of i?
It would be 7, since the inplace left shift was applied to a temporary int returned from the addition which gets assigned to r after that.
Continuing the assumption, the error may have the same effect (except for the value in i) as this expression:
int r = (i + 3) <<= 2;
But luckily, the compiler will complain, that he doesn't have an lvalue reference from (i + 3) to do the shift/assignment operation.
But play with this:
class Int
{
private:
int val;
public:
Int(int val) :
val(val)
{
}
Int operator+(const Int& other)const
{
return val + other.val;
}
Int operator+(int prim)const
{
return val + prim;
}
Int& operator+=(const Int& other)
{
val += other.val;
return *this;
}
//Error here, Int& would be correct
Int operator+=(int prim)
{
val += prim;
return *this;
}
Int operator<<(const Int& other)const
{
return val << other.val;
}
Int operator<<(int prim)const
{
return val << prim;
}
Int& operator<<=(const Int& other)
{
val <<= other.val;
return *this;
}
Int& operator<<=(int prim)
{
val <<= prim;
return *this;
}
operator int()const{
return val;
}
};
int main()
{
Int i = 4;
Int r = (i += 3) <<= 2;
cout << i;
return 0;
}
In C++ the * always means a value, in fact you can look to en English interpretation for these operators as follows:
&: the address of
*: the value of
So when you say &x it means "the address of x" and when you say *x it means "the value that x points to". So *this will always return a value.
Just be sure that the function itself that the hosts the returning is not a reference function. Please remember that in C++ you can create functions with the & or the * operators as well.