Example of DELPHI8 operator overloading

Source: Internet
Author: User
Tags class operator constructor
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.