1
votes

C++ allows to overload = operator only as a member function not as a global function.

Bruce Eckel says that if it was possible to define operator= globally, then you might attempt to redefine the built-in = sign. and due to this reason you can overload = operator only as a member function.

if C++ has already defined = operator then why the other operators like + - etc... are not defined by C++ as they can be overloaded as a non-member function. ?

5
The semantics of = are (usually) obvious. The semantics of the others are not -- what would - do for e.g. std::string? - ildjarn
Many many duplicates on this one, let me find one... - Xeo
that means only operator = is predefined by C++...is it so? - Amol Sharma
The copy assignment operator is automatically declared and defined if needed by the compiler. - Xeo
That rationale seems to be wrong (or at least very ambiguous). Yes, with operator overloading in general you could attempt to redefine built-in operators, just that the compiler won't let you. - UncleBens

5 Answers

4
votes

The compiler generates a default copy assignment operator (operator=) for all classes that do not define their own. That means that a global overload won't be selected under any circumstances.

0
votes

Because assignment has a clear meaning for any type, the other operators do not.

0
votes

Seems like it would be really hard to implement an operator=() from outside the class without reference to the specific implementation and the private members.

Operators like '+', however, sometimes have to be defined at the global level. Consider a complex number class. I can define complex::operator+(float) from inside my class, but I must define operator+(float, complex) from outside because the first operand doesn't have the type of my class.

Edit: I should have mentioned also that it's often easy to define global operators like operator+ without reference to specific implementation details by using other operators defined in the class. For example:

complex operator+(float lhs, complex rhs) {return complex(lhs, 0)+rhs;}
0
votes

To expand @ildjarn's comment, the semantics of the global built-in = are universally set; it is called when initializing a new instance of a class with an rvalue of some kind, and calls the appropriate constructor (based on the compile-time type of the rvalue), which is a nice way of providing c-style initialization instead of strictly requiring all variables to be initialized by an explicit call to a constructor. Overloading the global operator would change too much of the underlying semantics of the language while providing little to know real benefit--you'd be fundamentally changing how, which, and whether constructors are called, and if you feel like you have a reason to do that through overloading the global =, there's a good chance you're wrong.

The class-member operator= is overloadable because it's operating on an already existing and initialized lvalue of its class, and there are obvious cases where you would want custom behavior in modifying a pre-existing object (like only copying certain members from the rvalue while recalculating others and leaving still others alone, or properly disposing of resources held by pointer to take hold of a new resource without leaking memory).

Where the member operator= deals with a complete lvalue object, the built-in starts with nothing but an appropriately sized block of memory and an rvalue. Constructors of all shapes and forms are the language-provided interfaces for modifying the behavior of the built-in = without ceding control of the semantics themselves to you. I'd be interested to see what you feel you need to accomplish by overloading global = that couldn't be done with a constructor.

0
votes

The = operator (when used as initialization) is deeply tied to constructors; when you code SomeClass a = b; some constructor of SomeClass is called.