Ada-从异构列表中释放
我正在通过继承创建一个异构的循环链表。作为动态分配的数据类型,我需要某种形式的解除分配,所以我最初想到了Ada.Unchecked_Deallocation. 不幸的是,我的访问类型的工作方式是因为根元素被标记,并且因为我希望能够使用相同的指针类型来访问层次结构的任何对象,以下代码片段将无法编译。
type Element is tagged private;
type ElementPtr is access all Element'Class;
-- fully define Element
procedure Free is new Ada.Unchecked_Deallocation(Element, ElementPtr);
有没有人对我可以用来释放 ElementPtr 指向的任何继承 Element 类型的对象使用的内存的替代释放形式有任何建议?谢谢!
回答
Element和Element'Class是不同的类型,并且您尝试Unchecked_Deallocation使用不匹配的类型进行实例化。尝试实例化Element'Class:
procedure Free is new Ada.Unchecked_Deallocation(Element'Class, ElementPtr);