gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:05:46 25 4
gpt4 key购买 nike

是否可以按值调用带有参数的相同函数定义,然后在运行时通过引用调用?像这样的东西:

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

??

最佳答案

不是相同 功能,不是。您需要改用重载函数,例如:

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;

b := 'cde';
myfunc(b);
Caption := b; //prints 'cde'

b := 'cde';
myfunc(@b);
Caption := b; //prints 'abc'

关于delphi - 在 Delphi 中按值和按引用调用相同的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com