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. ?
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.
The =
operator (when used as initialization) is deeply tied to constructors; when you code SomeClass a = b;
some constructor of SomeClass
is called.
因为赋值对任何类型都有明确的含义,其他运算符则没有。
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;}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.