在Delphi中按值和引用调用相同的函数

dan*_*tei 2 delphi

是否可以通过参数按值和稍后在运行时调用相同的函数定义?就像是:

function myfunc(a:string);
begin
a:='abc';
end;
...
later:
b:='cde';
myfunc(b);
caption:=b; //prints 'cde'
...
later:
myfunc(@b);
caption:=b; //prints 'abc'
Run Code Online (Sandbox Code Playgroud)

??

Rem*_*eau 8

功能不一样,没有.您需要使用重载函数,例如:

function myfunc(a: string); overload;
begin
  // use a as needed, caller is not updated...
  a := 'abc';
end;

function myfunc(a: PString); overload;
begin
  // modify a^ as needed, caller is updated...
  a^ := 'abc';
end;
Run Code Online (Sandbox Code Playgroud)

b := 'cde';
myfunc(b);
Caption := b; //prints 'cde'
Run Code Online (Sandbox Code Playgroud)

b := 'cde';
myfunc(@b);
Caption := b; //prints 'abc'
Run Code Online (Sandbox Code Playgroud)