Can I overload operators for my own classes in Delphi?
I'm having a little trouble - I don't know if I can define my own operators for my classes. For example:
type
TMinMatrix = class(TMatrix)
private
RowAmount: Byte;
ColAmount: Byte;
Data: DataMatrix;
DemVector, SupVector: SupplyDemand;
public
constructor Create(Rows, Cols: Byte);
function GetRowAmount: Byte; override;
function GetColAmount: Byte; override;
destructor Destroy;
end;
How can I - or can't I :) - do something like:
TMinMatrix TMinMatrix::operator=(TMinMatrix* matr) (c++ code)
And by the way, can I define a copy constructor for my class?
source to share
The "traditional" method for copying classes in Delphi is to override the "AssignTo" TPersistant method. This usually takes the form
TSubclass(Dest).Field1 := Field1; TSubclass(Dest).Field2 := Field2;
It's a bit of a pain.
The CreateCopy constructor will then call this method:
constructor CreateCopy(ASource : TMyClass);
begin
Create;
Assign(ASource); // calls AssignTo
end;
Another trick in later versions of Delphi (working in 2006) is to use a record type to store fields.
class TMyClass = class(TPersistent)
protected
type // 2005+ only, otherwise use standalone record
TMyRecord = record
Name : string;
ID : integer;
end;
FData : TMyRecord;
procedure AssignTo(Dest : TPersistent);override;
public
property Name : string read FData.Name;
property ID: Integer read FData.ID;
end;
procedure TMyClass.AssignTo(Dest : TPersistent);
begin
if Dest is TMyClass then
TMyClass(Dest).FData := FData
else
inherited; // raise EConvertError
end;
It gets messy if you keep adding fields to subclasses - new post types need to be added, but it automatically handles new fields added to TMyrecord (don't forget to update AssignTo ())
source to share
type
TMinMatrix = class(TMatrix)
public
A : integer;
class operator Add( ATM, BTM : TMinMatrix ) : TMinMatrix;
// CTM := ATM + BTM
class operator Subtract( ATM, BTM : TMinMatrix ) : TMinMatrix;
// CTM := ATM - BTM;
end;
class operator TMinMatrix.Add( ATM, BTM : TMinMatrix ) : TMinMatrix;
begin
result := ATM.A + BTM.A;
end;
class operator TMinMatrix.Subtract( ATM, BTM : TMinMatrix ) : TMinMatrix;
begin
result := ATM.A - BTM.A;
end;
var
A, B, C : TMinMatrix;
begin
C := A + B; // calls Add()
C := B - A; // calls Subtract()
end.
Other operators:
Add Binary Add (a: type; b: type): resultType; + Subtract Binary Subtract (a: type; b: type): resultType; - Multiply Binary Multiply (a: type; b: type): resultType; * Divide Binary Divide (a: type; b: type): resultType; / IntDivide Binary IntDivide (a: type; b: type): resultType; div Modulus Binary Modulus (a: type; b: type): resultType; mod LeftShift Binary LeftShift (a: type; b: type): resultType; shl RightShift Binary RightShift (a: type; b: type): resultType; shr LogicalAnd Binary LogicalAnd (a: type; b: type): resultType; and LogicalOr Binary LogicalOr (a: type; b: type): resultType; or LogicalXor Binary LogicalXor (a: type; b: type): resultType; xor BitwiseAnd Binary BitwiseAnd (a: type; b: type): resultType; and BitwiseOr Binary BitwiseOr (a: type; b: type): resultType; or BitwiseXor Binary BitwiseXor (a: type; b: type): resultType; xor
;)
Operator overloading in Delphi Win32 only works for records, not classes.
It works with Delphi 2006 and up, but Delphi 2007 has bug fixes that make it easier to work with (related to function calls in statement results).
I gave a session on overloading write statements on CodeRage 3; You can get slides and sample code in 26326 CR3: Nullable Types w / Records, Methods and Operator Overloading and / or watch a video replay on DOWNLOAD VIDEO .
This was the session annotation:
Impossible Recorded Types, Methods, and Operator Overloading. One of the things in which data from databases and different Delphi types differ from each other is NULL support. When you work with databases a lot in Delphi, you want to have a data type that supports NULL. In the past, you've had to use variations, but no longer! With the introduction of operator overloading, you can do this with a record type too. This session shows how.
The reason for operator overloading is only possible for records in Delphi Win32 (i.e. not .NET), is that records are value types, so their memory management is not dynamic. Classes are reference types, so dynamic memory allocation is required: they require the concept of a garbage collector in order for operators to work with them.
Since there is no concept of a garbage collector in Delphi Win32, it is impossible to have operators for classes in Delphi Win32.
Please note that CodeRage 4 starts next week. It has a good lineup of speakers and sessions.
source to share
Delphi allows some functions or operators to be overloaded in record declarations. You can see it here: http://edn.embarcadero.com/article/34324
source to share