且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该如何调整我的代码以实现TBytes和TIdBytes之间的兼容性?

更新时间:2022-06-08 00:08:13

TBytes TIdBytes 都实现为动态数组,只是声明方式不同.政治上正确"的解决方案是复制字节.但这会浪费大型阵列的内存.一个简单的解决方案是使用类型转换,以便您可以利用数组的内部引用计数,例如:

TBytes and TIdBytes are both implemented as dynamic arrays, they are simply declared differently. The "politically correct" solution is to make a copy of the bytes. But that can waste memory for large arrays. A simpler solution is to use a typecast so you can utilize the array's internal reference count, eg:

type
  PIdBytes = ^TIdBytes;
var
  B1: TBytes;
  B2: TIdBytes;
begin
  B1 := ...;
  B2 := PIdBytes(@B1)^;
end;

或者简单地:

var
  B1: TBytes;
  B2: TIdBytes;
begin
  B1 := ...;
  B2 := TIdBytes(B1);
end;