官术网_书友最值得收藏!

Operator overloading

Your code can be more readable with operator overloading. You cannot overload the meaning of arbitrary operators in Object Pascal, only the built-in operators. You can also implement implicit conversion operators that make it possible to assign different types to a given type and define what would happen during the assignment. You can also define comparison operators to define the results of the built-in operators, =,<, and >.

Operator overloading leads to more compact, more readable code. For example, Delphi comes with a System.Math.Vectors unit with different useful types such as TVector3D with useful overloaded operations, as shown in the following code snippet:

uses 
  System.Math.Vectors; 
 
procedure DoSomeVectorMath; 
var 
  A, B, C: TVector3D; 
begin 
  A := TVector3D.Create(1,2,4); 
  B := TVector3D.Create(2,3,1); 
  C := A + B; 
  // ... 

Another good example is the TAlphaColorF record type defined in System.UITypes, which defines different operations on colors using real numbers.

Operator overloading cannot be used with class types, only with records.

主站蜘蛛池模板: 太原市| 宜宾市| 土默特右旗| 古浪县| 体育| 石林| 桐梓县| 襄城县| 师宗县| 那坡县| 海盐县| 泉州市| 西吉县| 南丰县| 古浪县| 新民市| 个旧市| 大冶市| 扶沟县| 迁西县| 滨州市| 青冈县| 东源县| 靖宇县| 隆昌县| 美姑县| 博乐市| 墨江| 碌曲县| 孟村| 隆尧县| 无锡市| 汨罗市| 巩义市| 龙山县| 琼中| 平乡县| 司法| 三门县| 和林格尔县| 定兴县|