Example of DELPHI8 operator overloading
Last Update:2017-02-27
Source: Internet
Author: User
unit WinForm; Interface uses System.Drawing, System.Collections, System.ComponentModel, System.Windows.Forms, System.Data; type Twinform = Class (System.Windows.Forms.Form) {$REGION ' Designer Managed Code '} Strict Private ///<summary> ///Required designer variable. ///</summary> Components:System.ComponentModel.Container; Button1:System.Windows.Forms.Button; ///<summary> ///Required to Designer support-do not modify ///The contents of this method with the Code editor. ///</summary> procedure InitializeComponent; procedure button1_click (sender:System.Object; e:system.eventargs); {$ENDREGION} Strict protected ///<summary> ///clean up any being used. ///</summary> procedure Dispose (Disposing:boolean); Override Private {Private Declarations} Public constructor Create; end; //Written class is OK, I use the record here. Because logging is a value type eliminates the hassle of creating an instance Tclasstest=record Public Fa:integer; //overloaded "+" operator class operator Add (a,b:tclasstest): tclasstest; end; [Assembly:runtimerequiredattribute (TypeOf (Twinform))] Implementation {$REGION ' Windows Form Designer generated code '} ///<summary> ///Required method for Designer support-does not modify ///The contents of this method with the Code editor. ///</summary> procedure twinform.initializecomponent; begin Self.button1: = System.Windows.Forms.Button.Create; self.suspendlayout; // //Button1 // Self.Button1.Location: = System.Drawing.Point.Create (96, 88); Self.Button1.Name: = ' Button1 '; Self.Button1.Size: = System.Drawing.Size.Create (392, 112); Self.Button1.TabIndex: = 0; Self.Button1.Text: = ' Button1 '; Include (Self.Button1.Click, Self.button1_click); // //Twinform // self.autoscalebasesize: = System.Drawing.Size.Create (6, 14); self.clientsize: = System.Drawing.Size.Create (560, 357); Self.Controls.Add (Self.button1); self.name: = ' twinform '; self.text: = ' WinForm '; self.resumelayout (False); End {$ENDREGION} procedure Twinform.dispose (Disposing:boolean); begin if disposing then begin If components <> nil then Components.dispose (); end; inherited Dispose (disposing); end; constructor Twinform.create; begin inherited Create; // //Required for Windows Form Designer support // InitializeComponent; // //Todo:add Any constructor code after InitializeComponent call // end; procedure Twinform.button1_click (sender:System.Object; e:system.eventargs); var a,b,c:tclasstest; begin A.FA: = 1; B.FA: = 2; c:=a+b; Two structures (or classes) with + operation, before DELHI8 is unthinkable System. Windows.Forms.MessageBox.Show (System.Convert.ToString (C.FA)); end; {tclasstest} //overload "+" operator implementation class operator Tclasstest.add (A, b:tclasstest): tclasstest; begin Result.fa:=a.fa + b.fa; end; end.