将指针的地址保存在变量中并访问它
我有一个在delphi中使用指针的初学者问题。
我创建一个对象并将其地址保存在变体变量中。 但我不知道如何使用这个变量访问对象。 这是我的代码:
...
New(PSplObj);
PSplObj^ := TsplCellObject.Create;
PSplObj.description := sTeam;
PSplObj.color := clRed;
myvar := Integer(PSplObj);
dispose(PSplObj);
// how to access the object throug the pointer in myvar?
...
I have a beginner question using pointers in delphi.
I create an object and save it's address in a variant variable.
But I dont know how to access the object using this variable.
Here's my code:
...
New(PSplObj);
PSplObj^ := TsplCellObject.Create;
PSplObj.description := sTeam;
PSplObj.color := clRed;
myvar := Integer(PSplObj);
dispose(PSplObj);
// how to access the object throug the pointer in myvar?
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的主要问题与 Delphi 对象引用是什么有关。考虑以下人工示例:
此时,我们有两个本质上相同的结构化类型的实例。然而,Delphi 呈现这些实例的方式却截然不同。记录是所谓的值类型,而对象是引用类型。
当您分配给引用类型的变量时,该值将被复制。例如:
值类型的示例包括整数、枚举类型、浮点类型、记录、对象等。
相反,分配给引用类型变量会复制引用,以便两个变量都引用同一对象。例如:
引用类型的一个特点是它们都是堆分配的,而值类型可以是堆分配的,也可以是堆栈分配的。
引用类型的示例包括类、接口、动态数组。
Delphi 字符串是一个有趣的混合体。尽管它们是作为引用实现的,但写入时复制使它们的行为类似于值类型。
Delphi 的对象语法隐藏了它们作为引用(即指针)实现的事实。
这一切意味着您的代码不必要地复杂。无需为对象引用分配存储空间,因为对象变量已经是引用。您可以这样写:
请注意,我使用的是 NativeInt,因为它是一个整数类型,定义的大小至少与指针相同。当 Delphi 的 64 位版本出现时,这一点将变得重要。
I think your main problem relates to what a Delphi object reference is. Consider the following artificial example:
At this point we have instances of two, essentially identical, structured types. However, the way Delphi presents these instances is quite different. The record is what is known as a value type and the object is a reference type.
When you assign to a variable of a reference type, the value is copied. For example:
Examples of value types include integers, enumerated types, floating point types, records, objects etc.
In contrast, assigning to a reference type variable copies the reference so that both variables refer to the same object. For example:
One feature of reference types is that they are all heap allocated, whereas value types can be either heap or stack allocated.
Examples of reference types include classes, interfaces, dynamic arrays.
Delphi strings are a funny hybrid. Although they are implemented as references, copy-on-write makes them behave like value types.
Delphi's syntax for objects hides the fact that they are implemented as a reference, i.e. a pointer.
What all this means is that your code is needlessly complicated. There is no need for you to allocate storage for an object reference since an object variable is already a reference. You can write it like this:
Note that I am using
NativeInt
since it is an integer type that is defined to be at least the same size as a pointer. This will become relevant when the 64 bit version of Delphi appears.首先,您实际上并不需要指向对象的指针,因为对象本身已经是引用类型,并且您可以将对象引用转换为
integer
(尽管NativeInt
> 可能会更好。)要使用它,您必须从变体中取出整数并将其转换回指针/对象类型,如下所示:First off, you don't really need a pointer to your object, because the object itself is already a reference type, and you can cast your object reference to
integer
(thoughNativeInt
might be better.) To use it, you have to get the integer back out of the variant and cast it back to your pointer/object type, like so: