【发布时间】:2011-11-05 23:00:42
【问题描述】:
是否可以将 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;
这可行,但如果我可以保留变量声明,我会很高兴。
function RS232_SendCommand(const Command: Integer): Boolean; overload;
var
Nothing: Pointer;
begin
Result := RS232_SendCommand(Command, Nothing, 0);
end;
解决方案是使用这样的东西。
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;
我这样做是因为我的一些命令在命令传输后发送了数据。
感谢您的帮助
【问题讨论】: