在C++中,我们可以让操作符为用户定义的类工作。这意味着C++有能力为操作符提供一个特殊的含义,这种能力被称为操作符重载。例如,我们可以在类似字符串的类中重载运算符“+”,这样我们就可以只使用+,将两个字符串连接起来。算术运算符可能重载的其他示例类有复数、小数、大整数等。
一个简单而完整的例子
CPP
#include<iostream> using namespace std; class Complex { private : int real, imag; public : Complex( int r = 0, int i = 0) {real = r; imag = i;} // This is automatically called when '+' is used with // between two Complex objects Complex operator + (Complex const &obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } void print() { cout << real << " + i" << imag << '' ; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; c3.print(); } |
输出:
12 + i9
操作员功能和正常功能之间有什么区别? 操作员功能与正常功能相同。唯一的区别是,运算符函数的名称始终是运算符关键字,后跟运算符符号,并且在使用相应的运算符时调用运算符函数。 下面是全局运算符函数的示例。
CPP
#include<iostream> using namespace std; class Complex { private : int real, imag; public : Complex( int r = 0, int i = 0) {real = r; imag = i;} void print() { cout << real << " + i" << imag << '' ; } // The global operator function is made friend of this class so // that it can access private members friend Complex operator + (Complex const &, Complex const &); }; Complex operator + (Complex const &c1, Complex const &c2) { return Complex(c1.real + c2.real, c1.imag + c2.imag); } int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; c3.print(); return 0; } |
12 + i9
我们能让所有操作员超负荷工作吗? 除了少数几个操作符外,几乎所有操作符都可以重载。以下是无法重载的运算符列表。
. (dot) :: ?: sizeof
为什么不能呢。(点),:,?:那么,是否会超载? 看见 这 从斯特劳斯托普自己那里得到答案。 关于运算符重载的要点 1) 要使运算符重载生效,至少一个操作数必须是用户定义的类对象。 2) 分配操作员: 编译器会自动为每个类创建一个默认赋值运算符。默认的赋值操作符会将右侧的所有成员赋值到左侧,并且在大多数情况下都可以正常工作(这种行为与复制构造函数相同)。看见 这 更多细节。 3) 转换运算符: 我们还可以编写转换运算符,用于将一种类型转换为另一种类型。
CPP
#include <iostream> using namespace std; class Fraction { private : int num, den; public : Fraction( int n, int d) { num = n; den = d; } // Conversion operator: return float value of fraction operator float () const { return float (num) / float (den); } }; int main() { Fraction f(2, 5); float val = f; cout << val << '' ; return 0; } |
输出:
0.4
重载的转换运算符必须是成员方法。其他运算符可以是成员方法或全局方法。 4) 任何可以用单个参数调用的构造函数都可以用作转换构造函数,这意味着它也可以用于对正在构造的类进行隐式转换。
CPP
#include <iostream> using namespace std; class Point { private : int x, y; public : Point( int i = 0, int j = 0) { x = i; y = j; } void print() { cout << "x = " << x << ", y = " << y << '' ; } }; int main() { Point t(20, 20); t.print(); t = 30; // Member x of t becomes 30 t.print(); return 0; } |
输出:
x = 20, y = 20 x = 30, y = 0
我们将很快讨论一些重要操作符的重载,如new、delete、逗号、函数调用、arrow等。 操作员超载测试
参考资料: http://en.wikipedia.org/wiki/Operator_overloading 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。