Good morning,

I've got a general question about overloading operators.

Suppose you have a class that overloads the + operator and overloads the = operator,

if you do something like this

length3 = length1 + length2,

will you automatically call both the overloaded + and = operator and if so, what is the order of operation, which would be called first?

Thank you,

7 Years
Discussion Span
Last Post by aikiart

Why don't you write some code and test it yourself?

Edited by Agni: n/a


To answer your question: If you have overloaded the said operators in your class definition, then yes, they will be called automatically.

For the second part of your question:
In C++, it can be generally assumed that all such statements follow operator precedence. As both binary addition ( + ) and subtraction ( - ) have a higher precedence than the assignment operator ( = ), the overloaded binary addition operator will be called first, followed by the assignment operator (this logically makes sense as well). Here is a table for operator precedence in C++.

Hope this helps!

Edited by amrith92: n/a


Thanks for your answer amrith92.

Agni, I agree, I took the lazy mans out when I should have just written the code to test it.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.