Skip to main content
added 222 characters in body; added 3 characters in body
Source Link
Motti
  • 115.5k
  • 56
  • 195
  • 277

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.

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.

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

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.

Source Link
Motti
  • 115.5k
  • 56
  • 195
  • 277

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.

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