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.

Dani AI

Generated

Short answer for : yes — the addition function is invoked to produce a (temporary) result, and then the assignment operator is invoked to place that result into the left-hand object. That follows how the expression is parsed (the binary + is grouped before =) and how assignment uses the value computed for its right-hand operand. (cppreference.com)

A couple of important language notes (these explain subtle behavior people often mix up). Operator precedence only controls parsing/grouping, not the exact runtime evaluation order of subexpressions. Before C++17 many operand evaluation orders were unspecified; C++17 tightened sequencing in several cases (for example, the right-hand side of an assignment is guaranteed to be evaluated before the left-hand side). That affects surprising examples with side effects, but it does not change the fact that the addition must produce a value that the assignment then consumes. (en.cppreference.com)

Practical implementation and style (common, robust pattern):

class Length {
    int value;
public:
    Length(int v=0): value(v) {}
    Length& operator+=(const Length& rhs) { value += rhs.value; return *this; }
    friend Length operator+(Length lhs, const Length& rhs) { lhs += rhs; return lhs; }
    Length& operator=(Length other) { using std::swap; swap(value, other.value); return *this; } // copy-and-swap
};

This keeps code simple, gives strong exception-safety via copy-and-swap, and lets operator+ reuse operator+= (a widely recommended idiom). In C++11 and later, a temporary RHS can prefer a move-assignment if one is available, so defining move-aware operations can avoid copies. (stackoverflow.com)

Testing tip and pitfalls: instrumenting operator+ and operator= with simple diagnostics will show the call order; watch for self-assignment and avoid returning const from operator= (it should return a non-const reference). Also prefer operator+= when possible for efficiency instead of repeatedly doing a = a + b. (cppreference.com)

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.