What happens when an assignment operator is :=
overloaded in Object Pascal? Basically, I mean what is first evaluated, and most importantly, how (if possible) I can change this order. Here is an example that bothers me:
I declare TMyClass
as follows:
TMyClass = class
private
FSomeString: string;
class var FInstanceList: TList;
public
function isValid: boolean;
property SomeString: String write setSomeString;
end;
The function isValid
checks MyObject for nil
and dangling pointers.
Now suppose I want to overload the statement :=
to assign a string to TMyClass. I also want to check if the object that I am assigning to this line is a real object, and if not creating a new one, then:
operator :=(const anewString: string): TMyClass;
begin
if not(result.isValid) then
result:= TMyObject.Create;
result.SomeString:= aNewString;
end;
In short, I was hoping the result would automatically hold the pointer to the object that I am assigning. But tests with the following:
procedure TForm1.TestButtonClick(Sender: TObject);
var
TestObject: TMyObject;
begin
TestObject:= TMyObject.Create;
TestObject:= 'SomeString';
TestObject.Free;
end;
, result
, TestObject
:=
.
, , , , - .
, , :=
, - ? ( , .)