can you explain me why this code doesn't work. operator+ override:
Fraction& Fraction::operator+(const Fraction& f) {
Fraction temp;
if (this->denominator == f.denominator){
temp.numerator = this->numerator + f.numerator;
temp.numerator = this->numerator;
temp.simplifier();
}
else {
temp.numerator = this->numerator * f.denominator + f.numerator * this->denominator;
temp.denominator = this->denominator * f.denominator;
temp.simplifier();
}
return temp;
}
operator= override:
void Fraction::operator=(const Fraction& f) {
this->numerator = f.numerator;
this->denominator = f.denominator;
}
after code
Fraction res;
res = f + g;
fields of res stay uninitialised. But, for example, code
Fraction res = g;
is working properly. So operator= doesn't understand (f + g) as one object? Thanks.
The problem is that your overload is returning a reference to an object, temp
, which is destroyed when the function returns.
Accessing that object after the function returns is undefined.
Return by value instead:
Fraction Fraction::operator+(const Fraction& f)
And
Fraction res = g;
isn't an assignment but an initialisation and will not use your assignment operator.
You're returning a reference to a temporary that was created on the stack. "temp", in operator+, has a lifetime that ends when operator+ returns. Its contents will likely be altered in chaotic ways, the next time a method is called.
In addition to return a reference to a temporary, the other problem you have is:
if (this->denominator == f.denominator){
temp.numerator = this->numerator + f.numerator;
temp.numerator = this->numerator; // *HERE*
temp.simplifier();
}
I am pretty sure the line labelled *HERE* should be:
temp.denominator = this->denominator;
Otherwise you will call simplifier with an uninitialized denominator and bad things will happen.
I would also recommend making operator +
a free standing binary operator which takes two Fraction
arguments - and make it use operator +=
. Note: lhs is passed as a copy, not as a const reference.
Fraction operator +(Fraction lhs, const Fraction &rhs)
{
lhs += rhs;
return lhs;
}
If you want to return "temp" as reference then you have to declare "temp" as:
static Fraction temp;
And now everything will work. Why because appending static
keyword before declaration made "temp" available until program termination..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.