本文介绍了为什么一些运算符只能作为成员函数重载,其他作为朋友函数,其余的作为两个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 为什么有些运算符只能作为成员函数重载,其他非成员自由函数和其余的作为两个?Why can some operators only be overloaded as member functions, other as non-member "free" functions and the rest of them as both? 如何记住哪些操作符可以重载为什么(成员,自由或两者)?How to remember which operators can be overloaded as what (member, free, or both)?推荐答案问题列出了三类运算符。我认为,把它们放在列表上有助于理解为什么有些操作符限制在可以重载的地方:The question lists three classes of operators. Putting them together on a list helps, I think, with understanding why a few operators are restricted in where they can be overloaded: 必须作为成员过载的操作员。这些是相当少的:Operators which have to be overloaded as members. These are fairly few: 作业 $ 运算符[]()下面的代码可以通过允许非成员函数调用操作符来进一步复杂化规则。 。使用有趣的索引类型,似乎可能会干扰对操作符的访问。虽然没有劫持重载的危险,但是编写高度非显而易见的代码似乎并没有太多的收获,但是有潜力。 类成员访问 operator->()。离线我看不到任何不良滥用的重载此运算符的非成员。另一方面,我也看不到任何。此外,类成员访问运算符具有相当特殊的规则,并且可能的过载干扰这些似乎是不必要的并发症。 The assignment operator=(). Allowing non-member assignments seems to open the door for operators hijacking assignments, e.g., by overloading for different versions of const qualifications. Given that assignment operators are rather fundamental that seems to be undesirable.The function call operator()(). The function call and overloading rules are sufficiently complicated as is. It seems ill-advised to complicate the rules further by allowing non-member function call operators.The subscript operator[](). Using interesting index types it seems that could interfere with accesses to operators. Although there is little danger of hijacking overloads, there doesn't seem to be much gain but interesting potential to write highly non-obvious code.The class member access operator->(). Off-hand I can't see any bad abuse of overloading this operator a non-member. On the other hand, I also can't see any. Also, the class member access operator has rather special rules and playing with potential overloads interfering with these seems an unnecessary complication.可能的是重载这些成员中的每一个是非成员(特别是对数组/指针工作的下标运算符,并且这些可以在调用的任一侧),似乎令人惊讶的是,例如,如果一个赋值可能被非-member overload这是比一个成员分配更好的匹配。这些运算符也非常不对称:你通常不希望支持在涉及这些运算符的表达式的两端进行转换。Although it is conceivable to overload each of these members are a non-member (especially the subscript operator which is works on arrays/pointers and these can be on either side of the call) it seems surprising if, e.g., an assignment could be hijacked by a non-member overload which which is a better match than one of the member assignments. These operators are also rather asymmetric: you generally wouldn't want to support conversion on both sides of an expression involving these operators.也就是说,例如,对于lambda表达式如果有可能重载所有这些操作符,我不会认为有一个内在的技术原因来防止这些操作符是可重载的。That said, e.g., for a lambda expression library it would be nice if it were possible to overload all of these operators and I don't think there is an inherent technical reason to preventing these operators from being overloadable. Operators which have to be overloaded as non-member functions. 用户定义的文本运算符The user-defined literal operator"" name()这个操作符是一个奇怪的球,运算符。在任何情况下,没有对象来调用这个成员可以定义成员:用户定义文字的左边参数总是内置类型。This operator is somewhat of an odd-ball and, arguably not really really an operator. In any case, there is no object to call this member on for which members could be defined: the left argument of user-defined literals are always built-in types.问题中没有提及,但也有一个不能重载的运算符:Not mentioned in the question but there are also operator which can't be overloaded at all: 成员选择器。 指向成员的对象访问操作符。 范围运算符 :: 三元运算符?: The member selector .The pointer-to-member object access operator .*The scope operator ::The ternary operator ?:这四个操作符被认为太根本了,以至于不能处理。虽然有一个建议允许重载运算符。()在某些时候没有强大的支持这样做(主要用例是智能引用)。 These four operators were considered to be too fundamental to be meddled with at all. Although there was a proposal to allow overloading operator.() at some point there isn't strong support doing so (the main use case would be smart references). Although there are certainly some contexts imaginable where it would be nice to overload these operators, too.可以作为成员或非成员重载的运算符。这是操作符的大部分:Operators which can be overloaded either as members or as non-members. This is the bulk of the operators: 前增量和后增量运算符++ / code>,运算符 - (), 运算符*() 运算符 - (int) 运算符和() 的[一元]地址 $ c> operator +(), operator - () 逻辑否定 operator!()(或 operator not()) c> operator〜()(或 operator compl()) 比较 operator ==(), operator!=(), >, operator>(), operator< =(), [binary]算术 operator +(), ,运算符*(), 运算符&()(或 operator bitand()), operator |()(或 >), operator ^()(或 operator xor()) 比特移位运算符<<()和 operator>>() 逻辑 operator ||()(或运算符或())和 operator&&&()(或运算符和()) code> operator @ =()( @ 是一个合适的运算符符号) 运算符()的序列(其中重载实际上会杀死序列属性!) 指针指向成员访问 operator-> *() 内存管理 operator new(), operator new [](),删除[]() The pre- and post-increment/-decrement operator++(), operator--(), operator++(int), operator--(int)The [unary] dereference operator*()The [unary] address-of operator&()The [unary] signs operator+(), operator-()The logical negation operator!() (or operator not())The bitwise inversion operator~() (or operator compl())The comparisons operator==(), operator!=(), operator<(), operator>(), operator<=(), and operator>()The [binary] arithmetic operator+(), operator-(), operator*(), operator/(), operator%()The [binary] bitwise operator&() (or operator bitand()), operator|() (or operator bit_or()), operator^() (or operator xor())The bitwise shift operator<<() and operator>>()The logic operator||() (or operator or()) and operator&&() (or operator and())The operation/assignment operator@=() (for @ being a suitable operator symbol()The sequence operator,() (for which overloading actually kills the sequence property!)The pointer pointer-to-member access operator->*()The memory management operator new(), operator new[](), operator new[](), and operator delete[]()可以作为成员或非成员重载的操作符不像其他操作者那样对于基本对象维护是必要的。这不是说他们不重要。事实上,这个列表包含几个运算符,它们是否是可重载的(例如,运算符&()的地址或通常导致排序的运算符,即运算符,(),运算符||()和运算符;()。The operators which can be overloaded either as members or as non-members are not as necessary for fundamental object maintenance as the other operators. That is not to say that they are not important. In fact, this list contains a few operators where it is rather questionable whether they should be overloadable (e. g., the address-of operator&() or the operators which normally cause sequencing, i. e., operator,(), operator||(), and operator&&().当然,C ++标准没有给出理由关于为什么事情以他们完成的方式完成(并且没有早期的记录,当这些决定做出时)。最好的理由可能在Bjarne Stroustrup的C ++的设计和进化中找到,我记得Of course, the C++ standard doesn't give a rationale on why things are done the way they are done (and there are also no records of the early days when these decisions where made). The best rationale can probably be found in "Design and Evolution of C++" by Bjarne Stroustrup. I recall that the operators were discussed there but there doesn't seem to be an electronic version available.总的来说,我不认为有其他限制的真正强有力的原因。但是,我怀疑这些限制可能会被取消,因为与现有软件的交互必然会以不可预测的方式改变某些程序的含义。Overall, I don't think there are really strong reasons for the restrictions other than potential complication which was mostly not considered worth the effort. I would, however, doubt that the restrictions are likely to be lifted as the interactions with existing software are bound to change the meaning of some program in unpredictable ways. 这篇关于为什么一些运算符只能作为成员函数重载,其他作为朋友函数,其余的作为两个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 09:41