[英]Overloading Operator = as a member function
C++ allows to overload =
operator only as a member function not as a global function. C ++允许重载
=
运算符仅作为成员函数而不是全局函数。
Bruce Eckel says that if it was possible to define operator= globally, then you might attempt to redefine the built-in = sign.
Bruce Eckel说
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. 并且由于这个原因,你可以将overload = operator仅作为成员函数。
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. 如果C ++已经定义了
=
运算符,那么为什么其他运算符如+
-
etc ...不是由C ++定义的,因为它们可以作为非成员函数重载。 ? ?
The compiler generates a default copy assignment operator (operator=) for all classes that do not define their own. 编译器为所有未定义自己的类生成默认的复制赋值运算符(operator =)。 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;
当你编写
SomeClass a = b;
some constructor of SomeClass
is called. 调用
SomeClass
一些构造函数。
因为赋值对任何类型都有明确的含义,其他运算符则没有。
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. 看起来很难从类外部实现operator =()而不需要引用特定的实现和私有成员。
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.
我可以在我的类中定义complex :: operator +(float),但是我必须从外部定义operator +(float,complex),因为第一个操作数没有我的类的类型。
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. 编辑 :我还应该提到,通过使用类中定义的其他运算符,通常很容易定义像operator +这样的全局运算符,而无需参考特定的实现细节。 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; 为了扩大@ ildjarn的评论,全球内置的语义
=
被普遍设置; 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. 在使用某种rvalue初始化类的新实例时调用它,并调用相应的构造函数(基于rvalue的编译时类型),这是提供c样式初始化而不是严格的好方法要求通过显式调用构造函数来初始化所有变量。 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). 类成员
operator=
是可重载的,因为它在其类的已存在且已初始化的左值上运行,并且在很明显的情况下,您需要自定义行为来修改预先存在的对象(例如仅从右值复制某些成员)重新计算其他人,让其他人独自离开,或者妥善处理指针所占用的资源,以便在不泄漏记忆的情况下掌握新资源。
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. 在成员
operator=
处理一个完整的左值对象的情况下,内置的东西只是一个适当大小的内存块和一个右值。 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. 我有兴趣看看你认为你需要通过重载global
=
来完成你无法用构造函数完成的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.