[fpc-devel] class operator overloads

Ryan Joseph ryan at thealchemistguild.com
Tue Apr 16 18:21:15 CEST 2019


Nagging question I had that I would like answered. I was told that we can’t have operator overloads on classes (only records) because there is a potential temporary class that could be created and thus some kind of automatic memory management would be needed. How is this any different than function operator overloads? If you allocated a new class in "operator +” then you would get a memory leak right? If that’s true then I don’t understand why we can’t have them in the actual class structure.

// these both do leak

operator + (left: TClass; right: T): TClass;
begin
  result := TClass.Create(right);
end;


class operator + (left: TClass; right: T): TClass;
begin
    result := TClass.Create(right);
end;

// these both don’t leak

operator + (left: TClass; right: T): TClass;
begin
  result := left;
end;


class operator + (left: TClass; right: T): TClass;
begin
    result := left;
end;

Regards,
	Ryan Joseph




More information about the fpc-devel mailing list