📄 14over.html
字号:
<html>
<head>
<title>Operator Overloading</title>
<meta name="description" content="Operator overloading in C++">
<meta name="keywords" content="operator overloading, operator, complex">
<link rel="stylesheet" href="../../rs.css">
</head>
<body background="../../images/margin.gif" bgcolor="#FFFFDC">
<!-- Main Table -->
<table cellpadding="6">
<tr>
<td width="78">
<td>
<h3>Operator overloading</h3>
<p>You can pretty much do any kind of arithmetic in C++ using the built-in integral and floating-point types. However, that's not always enough. Old-time engineers swear by Fortran, the language which has built-in type <i>complex</i>. In a lot of engineering applications, especially in electronics, you can't really do effective calculations without the use of complex numbers.
<p>C++ does not support complex arithmetics. Neither does it support matrix or vector calculus. Does that mean that engineers and scientists should stick to Fortran? Not at all! Obviously in C++ you can define new classes of objects, so defining a complex number is a piece of cake. What about adding, subtracting, multiplying, etc.? You can define appropriate methods of class <var>complex</var>. What about notational convenience? In Fortran you can add two complex numbers simply by putting the plus sign between them. No problem! Enter operator overloading.
<p>In an expression like
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>double delta = 5 * 5 - 4 * 3.2 * 0.1;</pre>
</table><!-- End Code -->
you see several arithmetic operators: the equal sign, the multiplication symbol and the minus sign. Their meaning is well understood by the compiler. It knows how to multiply or subtract integers or floating-point numbers. But if you want to teach the compiler to multiply or subtract objects of some user-defined class, you have to <i>overload</i> the appropriate operators. The syntax for operator overloading requires some getting used to, but the use of overloaded operators doesn't. You simply put a multiplication sign between two complex variables and the compiler finds your definition of complex multiplication and applies it. By the way, a <var>complex</var> type is conveniently defined for you in the standard library.
<p>An equal sign is an operator too. Like most operators in C++, it can be overloaded. Its meaning, however, goes well beyond arithmetics. In fact, if you don't do anything special about it, you can assign an arbitrary object to another object of the same class by simply putting an equal sign between them. Yes, that's right, you can, for instance, do that:
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>SymbolTable symTab1 (100);
SymbolTable symTab2 (200);
symTab1 = symTab2;</pre>
</table><!-- End Code -->
Will the assignment in this case do the sensible thing? No, the assignment will most definitely be wrong, and it will result in a very nasty problem with memory management. So, even if you're not planning on overloading the standard arithmetic operators, you should still learn something about the assignment operator; including when and why you would want to overload it. And that brings us to a very important topic--value semantics.
<br><a href="14value.html">Next.</a>
</table>
<!-- End Main Table -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -