Overload Operator (=);

My question is how do I get this function to return this instead of my temporary variable?

this being the variable that called the = function.
So if I put A = B, I'm looking for a.operator=(b). (Something like that.)

1
2
3
4
5
6
7
8
9
10
 Rational& Rational::operator=(const Rational & inputObj){
	std::cout << "Inside the = operator that accepts a object." << std::endl;
	Rational Temp(inputObj);
	Temp.simplifyInt();

	std::cout << Temp.numerator << "/" << Temp.denominator << std::endl;
	std::cout << Temp.equation << std::endl;

	return Temp;
}
Hi, its syntax of operator overloading, that you have must return .
And this is helpful in multi assign.like example a=b=c=d

That is a good convention to follow to be consistent with the behavior of operator= for built-in types.

With built-in types you can do something like this:

int a,b,c;
[...]
a=b=c=10;
If you do not return the reference to *this, the assignment chain won't be supported by your type.
Umm, your operator= needs to actually do the assignment operation.

So something similar to
1
2
3
4
5
6
7
8
Rational& Rational::operator=(const Rational& inputObj)
{
    numerator = inputObj.numerator;
    denominator = inputObj.denominator;
    equation = inputObj.equation;

    return *this;
}

But actually, that's what the default operator= will do anyways, so you probably don't need to define one yourself (unless you have pointer data members, or this is part of a homework assignment and you're required to define one).
Thanks for the replies!

@long double main

My Rational Temp(inputObj); code here is creating a temporary object, then copying the inputObj using a copy constructor.

Do I still need to individually define each "this" variable as you suggest long double main?

The reason I'm asked my original question is because Visual Studio is giving me the following warning: (*Not saying that after I've made the suggested changes to my function, it tells me this)

"returning address of local variable or temporary"

@ak16

I'm not sure I follow 100% of what your saying. It sounds like to me you are giving an explanation of why it is important to return this, instead of using a temporary variable. If this is the case, then thanks. Otherwise you may need to clarify your meaning.

Last edited on
This is what your assignment operator does right now:
1) Prints a line saying you're inside the = operator
2) Makes a copy of the right-hand side object (i.e. if you did A = B, then it makes a copy of B). Note that this copy is separate from this object and has nothing to do with it.
3) Calls simplifyInt on this copy (presumably, it should already be simplified?)
4) Prints the numerator, denominator, and equation of the copy
5) Returns a reference to the copy. Note that the copy is destroyed at the end of the function, so if you ever try to access it again, all sorts of crazy things could start happening.

Nowhere do you ever actually change the this object.
So if you write A = B; (where A and B are Rational objects), A will never actually change.
Last edited on
Thanks for the clarification! Changes have been made, and some problems solved.

I have another question if I may. Is there a way to refer to the whole object rather than its pieces of the function that is calling the operator?

A = B; A is calling the equals operator with B as the input. Is there a way inside the operator to refer to A as a whole, rather than as, numerator, denominator, equation.?


Yeah, that's what the this pointer is for.
(this points to the object that you called the member function on.)
yes you can do directly like -
*this = inputObj;

But with this you can only do SHALLOW COPY that compiler provide by default in "Default assignment operator".

Its worth when you have to do additional task before assigning or
Mainly while doing DEEP COPY.
Topic archived. No new replies allowed.