Here is something I have always wondered about. Assuming that I do not need to change the value of a parameter in a function/procedure, must I always explicitly declare a function or procedure parameter as a "const"? What bad things happen if I don't include "const"?
Having programmed in C++ for some years now, I really miss a stricter "const" in Delphi. In Delphi const only applies to the parameter, so you know it can't be assigned a new value.
ReplyDeleteHowever in C++ const can mean the object can't change. In order for this to work, you can declare methods as const, meaning they're guaranteed not to change the object (ie "self"). This I really miss, as it makes it a lot easier to reason about what some method will do which you haven't written yourself.
Martin Wienold I was not sure what Delphi have that leak, and just checked now: http://pastebin.com/BnRZS74m
ReplyDeleteYou are completely right
Martin Wienold Alexey Petushkov Oh that's just nasty. It works correctly if you wrap the constructor in a function though:
ReplyDeletefunction NewYoyop: IUnknown;
begin
result := TYoyop.Create;
end;
procedure Jojoba;
begin
DoSmthg(NewYoyop);
end;