【发布时间】:2011-06-07 09:35:14
【问题描述】:
Delphi 可以实现这样的功能吗? (带有字符串和记录的动态数组)
type
TStringArray = array of String;
TRecArray = array of TMyRecord;
procedure DoSomethingWithStrings(Strings : TStringArray);
procedure DoSomethingWithRecords(Records : TRecArray);
function BuildRecord(const Value : String) : TMyRecord;
DoSomethingWithStrings(['hello', 'world']);
DoSomethingWithRecords([BuildRecord('hello'), BuildRecord('world')]);
我知道它不会这样编译。只是想问是否有一个技巧可以得到类似的东西。
【问题讨论】:
-
请注意,写入
procedure DoSomethingWithStrings(Strings : TStringArray);将在堆栈上创建TStringArray参数的临时副本。您最好在此处添加const,即写procedure DoSomethingWithStrings(const Strings : TStringArray);