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

  • 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.

主站蜘蛛池模板: 临西县| 珠海市| 辽中县| 石楼县| 丽江市| 万源市| 大田县| 临高县| 徐水县| 宣恩县| 秀山| 安徽省| 米林县| 峨山| 清流县| 石阡县| 志丹县| 张家港市| 普格县| 木里| 蓝田县| 西和县| 临沧市| 宣威市| 盈江县| 忻城县| 合川市| 普定县| 福清市| 乌拉特中旗| 佳木斯市| 林州市| 翁牛特旗| 游戏| 静宁县| 丹巴县| 漠河县| 嘉黎县| 云霄县| 佛坪县| 安丘市|