【发布时间】:2021-05-31 06:33:57
【问题描述】:
在以下代码中:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers; use Ada.Containers;
with Ada.Containers.Vectors;
procedure Main is
package IEnumerators is
-- An umbrella class for all things that are iterable
type IEnumerator is interface;
procedure Move_Next (Self : in out IEnumerator) is abstract;
end IEnumerators;
use IEnumerators;
package IEnumerables is
-- The iterator for IEnumerator
type IEnumerable is interface;
function Get_Enumerator
(Self : IEnumerable) return IEnumerator is abstract;
end IEnumerables;
use IEnumerables;
package Lists is
-- An IEnumerable whose underlying is a vector
package Integer_Vector is new Ada.Containers.Vectors
(Element_Type => Integer, Index_Type => Positive, "=" => "=");
-- FAIL: type must be declared abstract or "Get_Enumerator" overridden
-- Fair enough, there's a problem with Get_Enumerator in the body
type List is new IEnumerable with record
Members : Integer_Vector.Vector;
end record;
end Lists;
package body Lists is
type List_Enumerator is new IEnumerator with null record;
overriding procedure Move_Next (Self : in out List_Enumerator) is
begin
null; -- do something
end Move_Next;
overriding
-- FAIL: subprogram "Get_Enumerator" is not overriding.
function Get_Enumerator (Self : List) return List_Enumerator
is
Result : List_Enumerator;
begin
return Result;
end Get_Enumerator;
end Lists;
begin
null;
end Main;
我不明白为什么覆盖的 Get-Enumerator 会失败;它与包 IEnumerables 中的签名相同(List 是一个具体的 IEnumerable,List_Enumerator 是一个具体的 IEnumerator)。
我哪里出错了?
【问题讨论】:
标签: inheritance interface overriding ada