delphi – 如何将“nil”常量传递给无类型参数?

前端之家收集整理的这篇文章主要介绍了delphi – 如何将“nil”常量传递给无类型参数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以将nil作为未声明的常量传递给某个函数的无类型参数?
我有这些函数,我想将一些常量传递给Data参数以满足编译器.在内部我是通过Size参数决定的.我知道我可以使用Pointer而不是无类型参数,但对我的情况来说它更舒服.

现在我收到E2250没有可以使用这些参数调用的’RS232_SendCommand’的重载版本

  1. function RS232_SendCommand(const Command: Integer): Boolean; overload;
  2. begin
  3. // is it possible to pass here an undeclared constant like nil in this example?
  4. Result := RS232_SendCommand(Command,nil,0);
  5. end;
  6.  
  7. function RS232_SendCommand(const Command: Integer; const Data; const Size: Integer): Boolean; overload;
  8. begin
  9. ...
  10. end;

这有效,但如果我能留下变量声明,我会很高兴.

  1. function RS232_SendCommand(const Command: Integer): Boolean; overload;
  2. var
  3. Nothing: Pointer;
  4. begin
  5. Result := RS232_SendCommand(Command,Nothing,0);
  6. end;

解决方案是使用这样的东西.

  1. function RS232_SendCommand(const Command: Integer): Boolean; overload;
  2. begin
  3. // as the best way looks for me from the accepted answer to use this
  4. Result := RS232_SendCommand(Command,nil^,0);
  5.  
  6. // or it also possible to pass an empty string constant to the untyped parameter
  7. // without declaring any variable
  8. Result := RS232_SendCommand(Command,'',0);
  9. end;

我这样做是因为我的一些命令在命令传输后发送了一个数据.

谢谢您的帮助

解决方法

简单:
  1. RS232_SendCommand(Command,0);

您只需要确保不从RS232_SendCommand内部访问Data参数,但可能是0 size参数的用途.

在我看来,这是最好的解决方案,因为它非常明确地表明您正在传递无法访问的内容.

猜你在找的Delphi相关文章