Ada 最终确定调整程序 - 这里放什么?
鉴于以下声明:
type Food_T is abstract tagged null record;
type Food_Ptr is access all Food_T'Class;
type Wrapper_T is new Ada.Finalization.Controlled with record
Ptr : Food_Ptr;
end record;
procedure Adjust (Object : in out Wrapper_T) is
begin
null; -- what goes here ?
end Adjust;
我想知道如何分配 &当我不知道 Object.ptr
将指向什么类型(以及源和目标在哪里!)时,分配(深度复制)food_
t 的正确派生。
任何帮助将不胜感激。
谢谢,
新气象局。
Given the following declarations :
type Food_T is abstract tagged null record;
type Food_Ptr is access all Food_T'Class;
type Wrapper_T is new Ada.Finalization.Controlled with record
Ptr : Food_Ptr;
end record;
procedure Adjust (Object : in out Wrapper_T) is
begin
null; -- what goes here ?
end Adjust;
I am wondering how to allocate & assign (Deep Copy) the correct derivitive of food_
t when i dont know what type Object.ptr
will be pointing to (and where Source & Destination are!).
Any help would be appreciated.
Thanks,
NWS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你的意思是:
那么当然,Object.Ptr.all 的工作就是确保它确实是一个深层副本。
(为此,Object.Ptr.all 的类型可能需要派生 Ada.Finalization.Controlled。为了实现此目的,您可能需要将 Food_T 设为 接口,以便 Food_T 派生类型也可以从 Ada.Finalization.Controlled 派生。)
I think you mean:
Then it's Object.Ptr.all's job to ensure that it is really a deep copy, of course.
(To do this, Object.Ptr.all's type might want to derive Ada.Finalization.Controlled. To allow this, you might want to make Food_T an interface so that a Food_T-deriving type can also derive from Ada.Finalization.Controlled.)
假设您有两个访问类型 T 的实例 A 和 B。当您执行 B := A 时,就会调用调整方法。
但是使用此方法时要小心,因为如果使用不当,它可能会导致内存泄漏!如果您的想法是 B 保存对对象 A 的完整新引用,则将其留空。在这种情况下,B 中的每个指针都将指向与 A 中的指针相同的内存位置。
如果要执行值类型赋值,也就是说,当您希望对象具有相同的“ data”但位于不同的内存位置,因此如果您更改 A,B 将不会注意到它。在这种情况下,您可以在调整方法内手动分配每个指针的值,并在必要时创建/释放内部对象。
Lets say you have two instances, A and B, of the access type T. The Adjust method is then called when you do B := A.
But be careful when using this method, since it can create memory leaks when not properly used! If your idea is B to hold a full new reference to object A, then leave it empty. In that case, every pointer within B will point to the same location in memory as the pointers within A.
Just complete the method if you want to perform value type assignments, that is to say, when you want the objects to have the same "data" but in different memory locations, so that if you change A, then B will not notice it. In that case you can manually assign the values of each pointer inside the Adjust method, and create/free the internal objects if necessary.