当我在程序中使用const
参数时,它有什么不同?当我在过程的参数中使用“const”时,它会有什么不同?
采取以下步骤,例如:
procedure DoSomething(Sender: TObject; const Text: String; var Reply: String);
begin
//Text is read-only and Reply will be passed back wherever DoSomething() was called
Reply:= Text;
end;
参数Text: String
与const
使得前缀(据我所知),该值的拷贝进行和使用 - 和是只读的。我想知道的是,如果不在那里放置const
,它会如何影响应用程序?也许是一种表演技巧?
在这里回答? http://stackoverflow.com/a/1601124/496736 – 2012-06-12 17:25:23
在大多数情况下,我认为将所有仅用于输入的参数标记为“const”的主要好处是,程序员将获得额外的帮助,避免愚蠢错误。 –
@Andreas令人讨厌的是'const'需要包含在接口和实现中。这是C++击败Delphi的一个领域。 –