9

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?

asked Nov 16, 2010 at 9:17
1

4 Answers 4

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.

answered Nov 16, 2010 at 9:27
Sign up to request clarification or add additional context in comments.

4 Comments

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, no that's not your question, that's another question, feel free to answer it but Charles (and others) have answered this question.
@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: stackoverflow.com has always been a Q & A site and very deliberately not a discussion site.
2

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.

e.g.

//a.h
class A { }; // compiler thinks A::operator= should be implicitly defined
//b.h
#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.

answered Nov 16, 2010 at 9:26

4 Comments

Then I would imagine operator+= also to be mandated to be a member
operator+= is not implicitly declared.
@flodin: function call operaor e.g. is mandated to be a member function. Why? Even that is not implicitly declared
@Chubsdad Well that's another (interesting) question. I don't know.
1

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.

answered Nov 16, 2010 at 9:28

Comments

1

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.

answered Nov 16, 2010 at 9:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.