德尔福“默认”关键字与旧版Delphi版本中的记录类型 [英] Delphi "default" keyword with Record types in older Delphi versions
问题描述
键入
TInstruction =记录
Archi:Byte; {CPUX32或者CPUX64! }
AddrMode:Byte; {地址模式}
地址:PByte;
VirtualAddr:PByte;
NextInst:PByte; {指向下一个指令}
OpCode:Byte; {OpCode Value}
OpType:Byte;
OpKind:Byte;
OpTable:Byte; {tbOneByte,tbTwoByte,...}
OperandFlags:Byte;
前缀:Word; {Prf_xxx}
...
end;
var
Inst:TInstruction;
begin
...
Inst:= default(TInstruction); //< -
Inst.Archi:= CPUX;
Pvt:= PPointer(AIntf)^; // vTable!
PCode:= PPointer(Pvt + Offset)^; //代码输入
Inst.NextInst:= PCode;
...
end;
default
关键字做了什么?
我假定如下:
FillChar(Inst,SizeOf(TInstruction),0 );
我的假设是否正确?
Default()
是一个无证的内在函数,用于支持泛型。 Delphi泛型的设计受到.net泛型的极大启发,您可能会从阅读.net的analagous文档中获益: https://msdn.microsoft.com/en-GB/library/xwth0h0d.aspx
Default()
是允许您默认初始化一个变量。当使用通用类型 Default()
允许您为类型为泛型的变量执行此操作。
如果要复制 Default()
的行为,请执行以下操作:
Finalize(Inst);
FillChar(Inst,SizeOf(Inst),0);
需要调用 Finalize
该类型被管理。也就是说,如果类型被管理,或者包含任何被管理的成员。托管类型包括字符串,动态数组,接口,变体,匿名方法等。
如果类型不包含托管类型,则调用 Finalize
可以省略。它不包括它,但是,因为编译器将消除它,如果不需要。如果您可以100%确定没有为托管类型分配值,那么您也可以忽略该调用。
默认初始化意味着以下内容:
- 数字类型为零。
- 枚举类型的序数为零的值。
-
False
用于布尔类型。 -
#0
- 字符串的空字符串。
-
Variant
的空变体。 li>
-
nil
为类,动态数组,接口和匿名方法。
I have this code in Delphi Detours library which I'm trying to port:
type
TInstruction = record
Archi: Byte; { CPUX32 or CPUX64 ! }
AddrMode: Byte; { Address Mode }
Addr: PByte;
VirtualAddr: PByte;
NextInst: PByte; { Pointer to the Next Instruction }
OpCode: Byte; { OpCode Value }
OpType: Byte;
OpKind: Byte;
OpTable: Byte; { tbOneByte,tbTwoByte,... }
OperandFlags: Byte;
Prefixes: Word; { Sets of Prf_xxx }
...
end;
var
Inst: TInstruction;
begin
...
Inst := default (TInstruction); // <-
Inst.Archi := CPUX;
Pvt := PPointer(AIntf)^; // vTable !
PCode := PPointer(Pvt + Offset)^; // Code Entry !
Inst.NextInst := PCode;
...
end;
What does the "default
" keyword do?
I assume something like:
FillChar(Inst, SizeOf(TInstruction), 0);
Is my assumption correct?
Default()
is an undocumented intrinsic function introduced to support generics. The design of Delphi generics was heavily inspired by .net generics and you might benefit from reading the analagous documentation for .net: https://msdn.microsoft.com/en-GB/library/xwth0h0d.aspx
The purpose of Default()
is to allow you to default initialize a variable. When working with generic types Default()
allows you to do so for a variable whose type is generic.
If you wish to replicate the behaviour of Default()
do the following:
Finalize(Inst);
FillChar(Inst, SizeOf(Inst), 0);
The call to Finalize
is needed in case the type is managed. That is if the type is managed, or contains any members that are managed. Managed types include strings, dynamic arrays, interfaces, variants, anonymous methods etc.
If the type does not contain managed types then the call to Finalize
may be omitted. It doesn't hurt to include it though, because the compiler will eliminate it if not needed. If you can be 100% certain that no managed types have been assigned a value then you could also omit that call.
Default initialization means the following:
- Zero for numeric types.
- The value with ordinal zero for enumerated types.
False
for boolean types.#0
for character types.- The empty string for strings.
- The empty variant for
Variant
. nil
for classes, dynamic arrays, interfaces and anonymous methods.
这篇关于德尔福“默认”关键字与旧版Delphi版本中的记录类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!