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.

Recommended Answers

All 3 Replies

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

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!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.