跳转到内容

运算符重载

本页使用了标题或全文手工转换
维基百科,自由的百科全书
(重定向自運算子多載

在计算机程序设计中,运算符重载(英語: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;
}

分类

[编辑]
操作符 不可重载 可重载
新增新的操作符
已有操作符重设

注释与引用

[编辑]
  1. ^ Stroustrup, Bjarne. Operator Overloading. C++ FAQ. [27 August 2020]. (原始内容存档于14 August 2011). 
  2. ^ Binary functions with a symbolic name can be called infix.
  3. ^ Predicate op/3. 
  4. ^ Hunt, John. Smalltalk and Object Orientation: An Introduction. Springer Science & Business Media. 6 December 2012. ISBN 978-1-4471-0961-7. 
  5. ^ Bertrand Meyer: Basic Eiffel language mechanisms. se.ethz.ch. [2021-04-07]. 
  6. ^ Operator functions in F90. www.mathcs.emory.edu. [2021-04-07]. 
  7. ^ Introduced in Fortran 90.
  8. ^ 3. Language Reference — Futhark 0.19.0 documentation. futhark.readthedocs.io. [2020-10-10]. 
  9. ^ 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. 
  10. ^ Type classes instead of overloading.
  11. ^ io guide. iolanguage.org. [2021-04-07]. 
  12. ^ Operators. 
  13. ^ Operators - R in a Nutshell, 2nd Edition [Book]. www.oreilly.com. [2021-04-07] (英语). 
  14. ^ Creating operators. 
  15. ^ Operators. Tour of Scala. 
  16. ^ Seed7 Manual: Structured syntax definition. seed7.sourceforge.net. [2020-09-29]. 
  17. ^ Swift: Advanced Operators. 
  18. ^ Why does Go not support overloading of methods and operators?. [4 September 2011]. 
  19. ^ 字符串使用“+”运算符串联一般不认为是运算符重载,而是编译器“魔法”,即将相应操作转译为StringBuilder类的调用。
  20. ^ Introduction. freepascal.org. [2020-09-30]. 
  21. ^ Operator Overloads. [28 September 2018]. 
  22. ^ 6.6 Overloading of Operators. Annotated Ada Reference Manual. 
  23. ^ Drayton, Peter; Albahari, Ben; Neward, Ted. C# in a Nutshell. O'Reilly Media, Inc. 2003. ISBN 978-0-596-00526-9. 
  24. ^ C++ Operator Overloading. 
  25. ^ Eclipse Ceylon: Operator Polymorphism. ceylon-lang.org. [2021-04-07]. 
  26. ^ Operator Overloading - D Programming Language. dlang.org. [2020-10-10]. 
  27. ^ A tour of the Dart language. dart.dev. [2020-09-30]. 
  28. ^ Operator Overloading. bourabai.kz. [2021-04-07]. 
  29. ^ The Apache Groovy programming language - Operators. groovy-lang.org. [2020-09-30]. 
  30. ^ Operator Overloading. Manifold. [7 June 2020]. 
  31. ^ Operator overloading. Kotlin. [24 June 2018]. 
  32. ^ Metamethods Tutorial. Lua-users Wiki. 
  33. ^ Implementing Operators for Your Class. [1 October 2013]. 
  34. ^ Operator Overloading. Free Pascal Manual. [1 December 2014]. 
  35. ^ Operator Overloading. Delphi Manual. [1 December 2014]. 
  36. ^ PHP magic methods overriding class properties. [7 April 2015]. (原始内容存档于4 March 2016). 
  37. ^ 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. 
  38. ^ 3. Data Model. The Python Language Reference. 
  39. ^ Methods. Official Ruby FAQ. 
  40. ^ Operator Overloading. Rust By Example. 
  41. ^ How to: Define an Operator (Visual Basic).