如何“全局”重载 opAssign 运算符在 C++
只是好奇如何让它们超载。
opAssign 运算符类似于 addAssign(+=) 和 subAssign(-=)。
“全局”意味着它们不重载为成员函数,而只是作用于操作数的运算符
对于这些 opAssign 运算符,它们是二元运算符。(它们接收两个操作数) 因此需要两个参数。
我在网上找不到例子......
Just curious about how to overload them.
The opAssign operators are like addAssign(+=) and subAssign(-=).
"globally" means they are not overloaded as member functions, but just a operator act on operands
For these opAssign operators, they are binary operators.(they receive two operands)
Therefore two parameters are needed.
I found no examples on the web.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是定义
operator+=
的简单示例:Here's a trivial example of defining
operator+=
:赋值运算符 (=) 的特殊之处在于,根据 C++ 标准的“§13.5.3 赋值”,它始终必须是非静态成员函数。
函数调用运算符和下标运算符也是如此。其他“赋值”运算符(+=、-=、*= 等)可以是自由的二元函数。
The assignment operator (=) is special in that it always needs to be a non-static member function as per "§13.5.3 Assignment" of the C++ standard.
The same is true for the function call operator and the subscript operator. Other "assignment" operators (+=, -=, *=, etc) can be free binary functions.