運算子多載
在電腦程式設計中,運算子多載(英語: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).