當我在程序中使用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的一個領域。 –