Delphi自指针使用

soa*_*oar 5 delphi pointers self

我需要在这个实例中获取指向我的类实例的指针.我不能直接使用"Self",我需要商店指针以备将来使用.我尝试了下一个代码:

type
    TTest = class(TObject)
    public
        class function getClassPointer: Pointer;
        function getSelfPointer: Pointer;
    end;

class function TTest.getClassPointer: Pointer;
begin
    Result := Pointer(Self);
end;

function TTest.getSelfPointer: Pointer;
begin
    Result := Pointer(Self);
end;
Run Code Online (Sandbox Code Playgroud)

结果都错了 - 这段代码:

test := TTest.Create;
Writeln('Actual object address: ', IntToHex(Integer(@test), 8));
Writeln('Class "Self" value: ', IntToHex(Integer(test.getClassPointer()), 8));
Writeln('Object "Self" value: ', IntToHex(Integer(test.getSelfPointer()), 8));
Run Code Online (Sandbox Code Playgroud)

收益:

Actual object address:    00416E6C
Class "Self" value:       0040E55C
Object "Self" value:      01EE0D10
Run Code Online (Sandbox Code Playgroud)

请帮助我理解,这个"自我"的价值是什么?"Self"是指向此类实例的指针吗?如何将此指针用于此对象之外的未来使用?如何从这个值获得正确的指针?

dth*_*rpe 12

你试图比较三个完全不同的实体.

@test返回变量test的地址,而不是它指向的对象实例.

test.getClassPointer()返回类元数据的地址,这是由编译器生成的常量数据结构,运行时可以在其中找到虚方法表,运行时类型信息表等.类的所有实例共享相同的类元数据结构.指向类元数据的指针是对象实例的类型标识 - 它是对象如何知道它在运行时的类型.

test.getSelfPointer()为您提供内存中对象实例的实际地址.两个对象实例(单独创建)将具有不同的实例地址.test.getSelfPointer()将等于测试实例变量的内容:指针(测试)

例如(伪代码,未测试):

type TTest = class
     end;

var test1: TTest;
    test2: TTest;

begin
  test1 = TTest.Create;  // allocates memory from the global heap, stores pointer
  test2 = test1;         // copies the pointer to the object into test2 variable
  writeln("Test1 variable points to: ", IntToHex(Integer(Pointer(test1))));
  writeln("Test2 variable points to: ", IntToHex(Integer(Pointer(test1))));
end.
Run Code Online (Sandbox Code Playgroud)

  • 这些是Delphi非常奇怪的结构.你确定自己没有让事情变得过于复杂吗?你的foo = TTest(指针(测试))就像说foo = test一样.Delphi中的每个对象实例变量实际上都是一个指针.指针就在表面之下. (4认同)