运算符重载

Multi tool use
多型 |
- 特设多态
- 函数重载
- 运算符重载
- 參數多态
- 雙重分派
- 多重分派
- 單一與動態分派
- 子类型
- 虛函數
|
|
在计算机程序设计中,运算符重载(英语:operator overloading)是多态的一种。这里,运算符(比如+,=或==)被当作多态函数,他们的行为随着其参数类型的不同而不同。运算符并不一定总是符号。
运算符重载通常只是一种语法糖。它可以简单地通过函数调用来模拟:
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;
}
分类
支持运算符重载和定义新运算符的语言:
- PostgreSQL的SQL方言
- Ruby
- Haskell
支持运算符重载的语言:
- Ada
- C++
- C#
- D
- Perl
- Python
Pico(某种程度上)
Pascal(仅Free Pascal Dialect)
- FreeBASIC
Visual Basic(需要Visual Basic .NET 2008或更高版本)
- Delphi
- Lua
不支持运算符重载的语言:
yLXdQ6az 3dhm1b,YeFNZhmQV j1vMa54duEoZGtcFDJvPhPG
Popular posts from this blog
Place in Moyen-Ogooué, Gabon Lambaréné Street in Lambaréné Lambaréné Location in Gabon Coordinates: 0°41′18″S 10°13′55″E / 0.68833°S 10.23194°E / -0.68833; 10.23194 Coordinates: 0°41′18″S 10°13′55″E / 0.68833°S 10.23194°E / -0.68833; 10.23194 Country Gabon Province Moyen-Ogooué Population (2013 census) • Total 38,775 Lambaréné is a town and the capital of Moyen-Ogooué in Gabon. With a population of 38,775 as of 2013, it is located 75 kilometres south of the equator. Lambaréné is based in the Central African Rainforest at the river Ogooué. This river divides the city into 3 districts: Rive Gauche, Ile Lambaréné and Rive Droite. The Albert Schweitzer Hospital and the districts Adouma and Abongo are located on Rive Droite. The districts Atongowanga, Sahoty, Dakar, Grand Village, Château, Lalala and Bordamur build the Ile Lambaréné. The majority of the people in Lambaréné live in the district Isaac located on Rive Gauche. This distr...
This article is about the number. For the year, see 800. For other uses, see 800 (disambiguation). Natural number ← 799 800 801 → List of numbers — Integers ← 0 100 200 300 400 500 600 700 800 900 → Cardinal eight hundred Ordinal 800th (eight hundredth) Factorization 2 5 × 5 2 Greek numeral Ω´ Roman numeral DCCC Binary 1100100000 2 Ternary 1002122 3 Quaternary 30200 4 Quinary 11200 5 Senary 3412 6 Octal 1440 8 Duodecimal 568 12 Hexadecimal 320 16 Vigesimal 200 20 Base 36 M8 36 800 ( eight hundred ) is the natural number following 799 and preceding 801. It is the sum of four consecutive primes (193 + 197 + 199 + 211). It is a Harshad number. Contents 1 Integers from 801 to 899 1.1 800s 1.2 810s 1.3 820s 1.4 830s 1.5 840s 1.6 850s 1.7 860s 1.8 870s 1.9 880s 1.10 890s 2 References Integers from 801 to 899 800s Main article: 801...
body.skin-minerva .mw-parser-output table.infobox caption{text-align:center} 維納斯堡 Venersborg (英文) CDP Venersborg, Washington 維納斯堡位置圖 坐标: 45°46′58″N 122°27′43″W / 45.7828°N 122.462°W / 45.7828; -122.462 國家 美國 州 華盛頓州 郡 克拉克县 面积 • 总计 27.8 平方公里(10.7 平方英里) • 陸地 27.8 平方公里(10.7 平方英里) • 水域 0.0 平方公里(0.0 平方英里) 海拔 155 米(509 英尺) 人口 (2000年) • 總計 3,274 • 密度 118.0/平方公里(305.6/平方英里) 时区 PST (UTC-8) • 夏时制 PDT(UTC-7) FIPS碼 53-74585 [1] GNIS編號 1527695 [2] 維納斯堡 ( 英语: Venersborg )是美国华盛顿州克拉克县的一個普查规定居民点(CDP),2000年美國人口普查時人口為2,085人。 最初為瑞典人聚居地。 參見 維納什堡 参考資料 ^ American FactFinder. United States Census Bureau. [ 2008-01-31 ] . ^ US Board on Geographic Names. United States Geological Survey. 2007-10-25 [ 2008-01-31 ] . 查 论 编 克拉克县 行政區劃 郡治 : 溫哥華 市 巴特爾格朗德 | 卡默斯 | 拉申特 | 里奇菲尔德 | 溫哥華 | 瓦休戈 | 林地市‡ 鎮 亞科爾特 CDP 安波伊 | 巴伯頓 | 布拉什普雷里 | 櫻桃樹叢區 | ...