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"?

Comments

  1. 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.

    However 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.

    ReplyDelete
  2. Martin Wienold I was not sure what Delphi have that leak, and just checked now: http://pastebin.com/BnRZS74m 
    You are completely right

    ReplyDelete
  3. Martin Wienold Alexey Petushkov Oh that's just nasty. It works correctly if you wrap the constructor in a function though:

    function NewYoyop: IUnknown;
    begin
      result := TYoyop.Create;
    end;
    procedure Jojoba;
    begin
      DoSmthg(NewYoyop);
    end;

    ReplyDelete

Post a Comment