TLa*_*ama 7 delphi untyped-variables
是否可以将nil作为未声明的常量传递给某个函数的无类型参数?我有这些函数,我想将一些常量传递给Data参数以满足编译器.在内部,我是通过Size参数决定的.我知道我可以使用Pointer而不是无类型参数,但对我的情况来说它更舒服.
现在我来了 E2250 There is no overloaded version of 'RS232_SendCommand' that can be called with these arguments
function RS232_SendCommand(const Command: Integer): Boolean; overload;
begin
// is it possible to pass here an undeclared constant like nil in this example?
Result := RS232_SendCommand(Command, nil, 0);
end;
function RS232_SendCommand(const Command: Integer; const Data; const Size: Integer): Boolean; overload;
begin
...
end;
Run Code Online (Sandbox Code Playgroud)
这有效,但如果我可以保留变量声明,我会很高兴.
function RS232_SendCommand(const Command: Integer): Boolean; overload;
var
Nothing: Pointer;
begin
Result := RS232_SendCommand(Command, Nothing, 0);
end;
Run Code Online (Sandbox Code Playgroud)
解决方案是使用这样的东西.
function RS232_SendCommand(const Command: Integer): Boolean; overload;
begin
// as the best way looks for me from the accepted answer to use this
Result := RS232_SendCommand(Command, nil^, 0);
// or it also possible to pass an empty string constant to the untyped parameter
// without declaring any variable
Result := RS232_SendCommand(Command, '', 0);
end;
Run Code Online (Sandbox Code Playgroud)
我这样做是因为我的一些命令在命令传输后发送了一个数据.
谢谢您的帮助
Dav*_*nan 12
简单:
RS232_SendCommand(Command, nil^, 0);
Run Code Online (Sandbox Code Playgroud)
您只需要确保不从内部访问Data参数RS232_SendCommand,但可能是0 size参数的用途.
在我看来,这是最好的解决方案,因为它非常明确地表明您正在传递无法访问的内容.