运算符重载
外观
(重定向自過載)
多态 |
---|
特设多态 |
參數多态 |
子类型 |
在计算机程序设计中,运算符重载(英語:operator overloading)是多态的一种。这里,运算符(比如+,=或==)被当作多态函数,它们的行为随着其参数类型的不同而不同。运算符并不一定总是符号。
简介
[编辑]运算符重载通常只是一种语法糖[1]。它可以简单地通过函数调用来模拟:
a + b * c
在一个支持运算符重载的语言里,上面的写法要比下面的写法有效而简练:
add(a, multiply(b, c))
(假设运算符* 的优先级高于运算符 +)
当一种语言允许运算符在某种情况下被隐式调用的时候,运算符重载将不只提供写法上的方便。例如,Ruby中的to_s
运算符就是如此,它將一个对象轉換為字符串。
用途
[编辑]运算符重载由于使程序员能够根据运算子类型的不同来决定运算符功能的不同而有多樣用途。C++中<<
的使用就是一个例子。表达式
a << 1
当a是整型变量时将返回a的两倍,但是当a是一个输出流时将向这个流中写入“1”。因为运算符重载允许程序员改变运算符通常的语义,慎重使用运算符重载通常被认为是一个好习惯。
簡易實例
[编辑]以下是C++语言示例:
#include <iostream>
using namespace std;
class point {
public:
int x, y;
point() {
x = y = 0;
}
point(int ix, int iy) {
x = ix;
y = iy;
}
point pointxyadd(point pi) {
return point(x + pi.x, y + pi.y);
}
point operator+(point pi) {
return point(x + pi.x, y + pi.y);
}
};
int main() {
point p1(5, 10), p2(8, 13), p3, p4;
p3 = p1.pointxyadd(p2);
p4 = p1 + p2;
cout << "p3 = (" << p3.x << ',' << p3.y << ')' << endl;
cout << "p4 = (" << p4.x << ',' << p4.y << ')' << endl;
return 0;
}
分类
[编辑]操作符 | 不可重载 | 可重载 |
---|---|---|
新增新的操作符 | ||
重设已有操作符 |
注释与引用
[编辑]- ^ Stroustrup, Bjarne. Operator Overloading. C++ FAQ. [27 August 2020]. (原始内容存档于14 August 2011).
- ^ Binary functions with a symbolic name can be called infix.
- ^ Predicate op/3.
- ^ Hunt, John. Smalltalk and Object Orientation: An Introduction. Springer Science & Business Media. 6 December 2012. ISBN 978-1-4471-0961-7.
- ^ Bertrand Meyer: Basic Eiffel language mechanisms. se.ethz.ch. [2021-04-07].
- ^ Operator functions in F90. www.mathcs.emory.edu. [2021-04-07].
- ^ Introduced in Fortran 90.
- ^ 3. Language Reference — Futhark 0.19.0 documentation. futhark.readthedocs.io. [2020-10-10].
- ^ Smith, Chris. Programming F# 3.0: A Comprehensive Guide for Writing Simple Code to Solve Complex Problems. O'Reilly Media, Inc. 9 October 2012. ISBN 978-1-4493-2604-3.
- ^ Type classes instead of overloading.
- ^ io guide. iolanguage.org. [2021-04-07].
- ^ Operators.
- ^ Operators - R in a Nutshell, 2nd Edition [Book]. www.oreilly.com. [2021-04-07] (英语).
- ^ Creating operators.
- ^ Operators. Tour of Scala.
- ^ Seed7 Manual: Structured syntax definition. seed7.sourceforge.net. [2020-09-29].
- ^ Swift: Advanced Operators.
- ^ Why does Go not support overloading of methods and operators?. [4 September 2011].
- ^ 字符串使用“+”运算符串联一般不认为是运算符重载,而是编译器“魔法”,即将相应操作转译为StringBuilder类的调用。
- ^ Introduction. freepascal.org. [2020-09-30].
- ^ Operator Overloads. [28 September 2018].
- ^ 6.6 Overloading of Operators. Annotated Ada Reference Manual.
- ^ Drayton, Peter; Albahari, Ben; Neward, Ted. C# in a Nutshell. O'Reilly Media, Inc. 2003. ISBN 978-0-596-00526-9.
- ^ C++ Operator Overloading.
- ^ Eclipse Ceylon: Operator Polymorphism. ceylon-lang.org. [2021-04-07].
- ^ Operator Overloading - D Programming Language. dlang.org. [2020-10-10].
- ^ A tour of the Dart language. dart.dev. [2020-09-30].
- ^ Operator Overloading. bourabai.kz. [2021-04-07].
- ^ The Apache Groovy programming language - Operators. groovy-lang.org. [2020-09-30].
- ^ Operator Overloading. Manifold. [7 June 2020].
- ^ Operator overloading. Kotlin. [24 June 2018].
- ^ Metamethods Tutorial. Lua-users Wiki.
- ^ Implementing Operators for Your Class. [1 October 2013].
- ^ Operator Overloading. Free Pascal Manual. [1 December 2014].
- ^ Operator Overloading. Delphi Manual. [1 December 2014].
- ^ PHP magic methods overriding class properties. [7 April 2015]. (原始内容存档于4 March 2016).
- ^ Orwant, Jon. Computer Science & Perl Programming: Best of The Perl Journal. O'Reilly Media, Inc. 4 November 2002: 347–. ISBN 978-0-596-00310-4.
- ^ 3. Data Model. The Python Language Reference.
- ^ Methods. Official Ruby FAQ.
- ^ Operator Overloading. Rust By Example.
- ^ How to: Define an Operator (Visual Basic).