Take the 2-minute tour ×

Why is overloaded operator= mandated to be a member function ($13.5.3), but not a compound assignment operator e.g. operator+= ($13.5.2)? Am I overlooking something here?

share|improve this question

4 Answers 4

A copy assignment operator=, as a member, is always provided by the compiler if the user doesn't define one. I believe that it was only for simplicity and to avoid unexpected ambiguities that it was made a requirement that operator= can't be defined as a free function.

Conversion operators take care of the case when you want to assign from a user-defined type to a built-in type.

share|improve this answer
So my question I guess now is why certain operators are mandated to be overloaded as members e.g. operator [], operator() while others are not? –  Chubsdad Nov 16 '10 at 9:34
@Chubsdad, no that's not your question, that's another question, feel free to answer it but Charles (and others) have answered this question. –  Motti Nov 16 '10 at 9:37
@Motti: Not sure if this happens to you or not, but it happens to me at least. While discussing and sharing, a lot more clarity emerges about what one wants –  Chubsdad Nov 16 '10 at 9:38
@Chubsdad: stackoverflow.com has always been a Q & A site and very deliberately not a discussion site. –  Charles Bailey Nov 16 '10 at 9:43

The sections you reference have this to say about hiding base class implementations of operator=

Because a copy assignment operator operator= is implicitly declared for a class if not declared by the user (12.8),

This may also be the answer to your question, since the compiler needs to know if it should generate an operator= it must know if such an operator was defined, if it could be defined outside the class the compiler couldn't know if it was defined in a different translation unit.


class A { }; // compiler thinks A::operator= should be implicitly defined

#include "a.h"
A& operator=(A& This, const A& other) { /*...*/ } // Oops it's explicitly defined

Compound operators, on the other hand, are not implicitly defined therefore there is no reason to force them to be declared as member functions.

share|improve this answer
Then I would imagine operator+= also to be mandated to be a member –  Chubsdad Nov 16 '10 at 9:27
operator+= is not implicitly declared. –  flodin Nov 16 '10 at 9:28
@flodin: function call operaor e.g. is mandated to be a member function. Why? Even that is not implicitly declared –  Chubsdad Nov 16 '10 at 9:30
@Chubsdad Well that's another (interesting) question. I don't know. –  flodin Nov 16 '10 at 19:05

Along with default and copy constructors, operator= is also treated specially in C++. That means, even if you don't declare one, the compiler will provide a default implementation for you. But default implementations are not always the behaviour that suits your class' needs, that's why you should declare them explicitly (or hide them, by assigning private visibility).

And why is default constructor, copy constructor and assignment operator so special? Because they are involved in standard variable initialization and parameter passing: when you pass a class-typed parameter to a function by value (not by reference or pointer), these operations are called to copy it's content to the stack.

share|improve this answer

As stated by Charles, a copy assignment operator= is always provided by the compiler if the user doesn't define one. This compiler provided member function would always have precedence over a non-member function, so even if you could define it as a non-member function, it wouldn't be called.

share|improve this answer

Your Answer


