delphi – 如何将“nil”常量传递给无类型参数?
发布时间:2020-12-15 04:15:16 所属栏目:大数据 来源:网络整理
导读:是否可以将nil作为未声明的常量传递给某个函数的无类型参数? 我有这些函数,我想将一些常量传递给Data参数以满足编译器.在内部我是通过Size参数决定的.我知道我可以使用Pointer而不是无类型参数,但对我的情况来说它更舒服. 现在我收到E2250没有可以使用这些
是否可以将nil作为未声明的常量传递给某个函数的无类型参数?
我有这些函数,我想将一些常量传递给Data参数以满足编译器.在内部我是通过Size参数决定的.我知道我可以使用Pointer而不是无类型参数,但对我的情况来说它更舒服. 现在我收到E2250没有可以使用这些参数调用的’RS232_SendCommand’的重载版本 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; 我这样做是因为我的一些命令在命令传输后发送了一个数据. 谢谢您的帮助 解决方法
简单:
RS232_SendCommand(Command,0); 您只需要确保不从RS232_SendCommand内部访问Data参数,但可能是0 size参数的用途. 在我看来,这是最好的解决方案,因为它非常明确地表明您正在传递无法访问的内容. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |