Operator Overloading of Delphi

Source: Internet
Author: User
Tags class operator integer division

If you use Delphi to write a game, the only thing that is not convenient for C ++ is that it does not support Operator overloading. When you write a vector or matrix computingProgramIt is very difficult to use Delphi that does not support Operator overloading.
But Delphi 2006 has changed this situation. The New Delphi kernel already supports Operator overloading!
This article describes how to use the operator overload function of Delphi.

Note: Only Delphi 2006 and the free version of Turbo Delphi support this function!

Delphi for Win32 only supports record-type operators, while delphi. Net also supports class-type operators.
The following are reload operators supported by Delphi:
Operator type declaration syntax use symbols
Implicit conversion implicit (A: type): resulttype; implicit conversion

Explicit conversion explicit (A: type): resulttype; explicit conversion

Negative (A: type): resulttype ;-

Positive (A: type): resulttype; +

Incremental unary operation Inc (A: type): resulttype; Inc

Decimal unary calculation Dec (A: type): resulttype; Dec

Logicalnot (A: type): resulttype; not

Bitwisenot (A: type): resulttype; not

Trunc (A: type): resulttype; trunc

Round a unary operation round (A: type): resulttype; round

Equal to equal (A: type; B: type): Boolean; =

Not equal to notequal (A: type; B: type): Boolean; <>

Greater than greaterthan (A: type; B: type) Boolean;>

Greater than or equal to greaterthanorequal (A: type; B: type): resulttype;> =

Less than lessthan (A: type; B: type): resulttype; <

Less than or equal to lessthanorequal (A: type; B: type): resulttype; <=

Add (A: type; B: type): resulttype; +

Subtract (A: type; B: type): resulttype ;-

Multiplier binary operation multiply (A: type; B: type): resulttype ;*

Divide (A: type; B: type): resulttype ;/

Integer Division binary operation intdivide (A: type; B: type): resulttype; Div

Modulo binary operation modulus (A: type; B: type): resulttype; mod

Left shift binary operation shiftleft (A: type; B: type): resulttype; SHL

Right Shift binary operation shiftright (A: type; B: type): resulttype; SHR

Logical and binary operations logicaland (A: type; B: type): resulttype; and

Logical or binary operation logicalor (A: type; B: type): resulttype; or

Logical XOR or binary operation logicalxor (A: type; B: type): resulttype; XOR

Bitwiseand (A: type; B: type): resulttype; and

Bitwiseor (A: type; B: type): resulttype; or

Bitwisexor (A: type; B: type): resulttype; XOR

Before introducing how to use Operator overloading, let's talk about the new features of record in Delphi.
In delphi2006, record is already very similar to class, for example
Type
Tmyrecord = record
Private
X: integer;
Public
Constructor create (VAL: integer );
Procedure change (Arg: integer); overload;
Procedure change (arg1, arg2: integer); overload;
End;

the difference between record and class is that
record does not support inheritance.
record contains variable parts. class does not. (For variable parts, see the Help File of delphi2006)
record is a value type; class is a reference type (For details, refer to the Help File of delphi2006)
record is allowed in Win32 and. NET platform supports Operator overloading; Class is only in. net supports
automatic record construction. The default non-parameter constructor is used, and the class must be explicitly constructed. Because record has default non-parameter constructor, therefore, a user-defined constructor must have at least one parameter
NO destructor in record
record does not support virtual methods
record does not support interface implementation in Win32, in. net support

well, let's see how the operator overload is implemented.
first, the syntax is as follows:
type
typename = [class | Record] // only used. class
class operator conversionop (A: type): resulttype;
class operator unaryop (A: type): resulttype;
class operator comparisonop (A: type; B: type): Boolean;
class operator binaryop (A: type; B: type): resulttype;
end;
the syntax of the implementation part is as follows:
class operator typename. conversionop (A: type): resulttype;
class operator typename. unaryop (A: type): resulttype;
class operator typename. comparisonop (A: type; B: type): Boolean;
class operator typename. binaryop (A: type; B: type): resulttype;

Let's take a look at the specific example.
Type
Tvector = record
X: single;
Y: single;
Z: single;
Public
Constructor create (x1, Y1, Z1); // This constructor does not need to be written. The reason will be explained later.
Class operator add (V1, V2: tvector): tvector; // overload operator +
Class operator implicit (I: single): tvector; overload; // implicit conversion
Class operator implicit (I: tvector): tvector; overload; // same as above
End;

// Specific implementation
{Tvector}

Constructor tvector. Create (x1, Y1, Z1: single );
Begin
X: = x1;
Y: = Y1;
Z: = Z1;
End;

Class operator tvector. Add (V1, V2: tvector): tvector;
Begin
Result. X: = v1.x + v2.x;
Result. Y: = v1.y + v2.y;
Result. Z: = v1.z + v2.z;
Result. W: = v1.w + v2.w;
End;

Class operator tvector. Implicit (I: single): tvector;
Begin
Result. X: = I;
Result. Y: = I;
Result. Z: = I;
Result. W: = I;
End;

Class operator tvector. Implicit (I: tvector): tvector;
Begin
Result. X: = I. X;
Result. Y: = I. Y;
Result. Z: = I. Z;
Result. W: = I. W;
End;

// Call Method
...
VaR
V1, V2: tvector;
Begin
...
V1.create (1, 2, 3 );
// You can also assign values to X, Y, and Z using the following method:
// V1.x: = 1;
// V1.y: = 2;
// V1.z: = 3;
// Do not use the CREATE FUNCTION
// Unlike the class, the record can be automatically initialized for members.
// In this example, the initialization values of X, Y, and Z are 0.

V2: = 1; // class operator implicit (I: tvector): tvector
// In this case, all V2 members are assigned a value of 1.
V2: = V1; // class operator implicit (I: tvector): tvector

V2: = V1 + V2; // class operator add (V1, V2: tvector): tvector;

V2: = V2 + 1; // class operator add (V1, V2: tvector): tvector
// Composite call with class operator implicit (I: tvector): tvector

End;

This implementation method of Delphi is obviously more convenient than C ++, because you do not need to consider memory release, or you do not need to define constructors.
 

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.