是否可以将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 …Run Code Online (Sandbox Code Playgroud) 覆盖Free方法是否安全?我问,因为我想使用这样的东西.我知道使用TerminateThread是非常难看的,但在我的情况下,立即使用其所有线程终止进程非常关键.
我已经看到在System.pas中声明的__free方法,但我不知道它是否与TObject.Free方法有关,这就是为什么我问它是否安全的原因.
type
TMyThread = class(TThread)
private
destructor Destroy; override;
public
procedure Execute; override;
constructor Create;
procedure Free(const Force: Boolean = False); // is it safe to use this?
end;
procedure TMyThread.Free(const Force: Boolean = False);
begin
if not Force then
begin
Terminate;
WaitFor;
end
else
TerminateThread(Handle, 0);
inherited Free;
end;
destructor TMyThread.Destroy;
begin
// free resources etc.
inherited Destroy;
end;
Run Code Online (Sandbox Code Playgroud)
谢谢