運算符重載
外觀
多態 |
---|
特設多態 |
參數多態 |
子類型 |
在計算機程序設計中,運算符重載(英語: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).