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

  • Expert Delphi
  • Pawe? G?owacki
  • 157字
  • 2021-07-02 20:44:24

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.

主站蜘蛛池模板: 巴马| 黄大仙区| 屯留县| 丰县| 黄大仙区| 临沭县| 白银市| 囊谦县| 红桥区| 丹寨县| 荔浦县| 金昌市| 皋兰县| 焦作市| 林芝县| 德清县| 余江县| 永吉县| 大兴区| 孙吴县| 莱阳市| 涿鹿县| 吉水县| 炎陵县| 封丘县| 鄂尔多斯市| 冕宁县| 丹江口市| 金寨县| 荥阳市| 元氏县| 云安县| 交城县| 彭山县| 曲水县| 鄱阳县| 海南省| 额尔古纳市| 江口县| 出国| 桦川县|