问题描述
我正在通过继承创建一个异构的循环链表。作为动态分配的数据类型,我需要某种形式的解除分配,所以我最初想到了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);