0

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,
A.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by aikiart
0

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

Edited by Agni: n/a

0

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

0

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.

Art

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.