如何在Ada中从另一个包传递私有类型
假设我有一个如下所示的包:如何在Ada中从另一个包传递私有类型,ada,Ada,假设我有一个如下所示的包: package binary_search_tree is type pointer is limited private; procedure insert (item : pointer); private type node; type pointer is access node; end binary_search_tree; 现在让我们假设我有一个司机想这样称呼foo: with binary_search_tree;
package binary_search_tree is
type pointer is limited private;
procedure insert (item : pointer);
private
type node;
type pointer is access node;
end binary_search_tree;
现在让我们假设我有一个司机想这样称呼foo:
with binary_search_tree;
procedure main is
package new_tree is new binary_search_tree;
begin
new_binary_search_tree.insert(???);
end
所以我要做的是创建一个二进制搜索树包并在驱动程序中实现它。规范要求我在二叉树包中将节点指针类型声明为有限私有,它希望我通过驱动程序将指向二叉树根的指针传递到其函数中。但是,如果指针类型是私有的,我不确定如何才能做到这一点。除非我遗漏了什么,否则我不会认为我应该从一开始就这么做。这是否违反了封装?您的
指针
类型位于二进制搜索树
的可见部分,因此任何实例化都有一个类型指针
。它是有限私有的
,因此您可以声明该类型的变量;您可以将它们传递给子程序(例如insert
),但不能相互比较、复制它们或使用任何内容知识
您的泛型(没有任何实际细节,但可编译)如下所示
generic
type Element is private;
package Binary_Search_Tree is
type Tree is limited private;
procedure Insert (Into : in out Tree; Item : Element);
private
type Node;
type Tree is access Node;
type Node is record
-- pointer stuff
Value : Element;
end record;
end Binary_Search_Tree;
with Binary_Search_Tree;
procedure Main is
package New_Tree is new Binary_Search_Tree (Integer);
T : New_Tree.Tree;
begin
New_Tree.Insert (T, 123);
end Main;
用户程序(这就是您所说的“驱动程序”?)的外观
generic
type Element is private;
package Binary_Search_Tree is
type Tree is limited private;
procedure Insert (Into : in out Tree; Item : Element);
private
type Node;
type Tree is access Node;
type Node is record
-- pointer stuff
Value : Element;
end record;
end Binary_Search_Tree;
with Binary_Search_Tree;
procedure Main is
package New_Tree is new Binary_Search_Tree (Integer);
T : New_Tree.Tree;
begin
New_Tree.Insert (T, 123);
end Main;
您的
指针
类型位于二进制搜索树
的可见部分,因此任何实例化都有一个类型指针
。它是有限私有的
,因此您可以声明该类型的变量;您可以将它们传递给子程序(例如insert
),但不能相互比较、复制它们或使用任何内容知识
您的泛型(没有任何实际细节,但可编译)如下所示
generic
type Element is private;
package Binary_Search_Tree is
type Tree is limited private;
procedure Insert (Into : in out Tree; Item : Element);
private
type Node;
type Tree is access Node;
type Node is record
-- pointer stuff
Value : Element;
end record;
end Binary_Search_Tree;
with Binary_Search_Tree;
procedure Main is
package New_Tree is new Binary_Search_Tree (Integer);
T : New_Tree.Tree;
begin
New_Tree.Insert (T, 123);
end Main;
用户程序(这就是您所说的“驱动程序”?)的外观
generic
type Element is private;
package Binary_Search_Tree is
type Tree is limited private;
procedure Insert (Into : in out Tree; Item : Element);
private
type Node;
type Tree is access Node;
type Node is record
-- pointer stuff
Value : Element;
end record;
end Binary_Search_Tree;
with Binary_Search_Tree;
procedure Main is
package New_Tree is new Binary_Search_Tree (Integer);
T : New_Tree.Tree;
begin
New_Tree.Insert (T, 123);
end Main;
它看起来好像
Binary\u Search\u Tree
是泛型的。什么是通用的<例如,代码>整数?您肯定不想插入指针?在Main
中,在new\u binary\u search\u tree.foo(???)中的foo
是什么代码>?你的意思是项目
?是的,foo应该是insert。我道歉。是的,包装应该是通用的。在我的实际实现中,它有两个通用参数:key和tree\u record。当我在主过程中实现它时,我应该传递一个键(名称)的字符串,tree_记录将成为一个带有名称和电话号码的记录,这两个都是字符串。另外,不,我不打算插入指针。作为参数传递的指针应该指向我希望插入新节点的树的根。它看起来似乎是泛型的Binary\u Search\u tree
。什么是通用的<例如,代码>整数
?您肯定不想插入指针?在Main
中,在new\u binary\u search\u tree.foo(???)中的foo
是什么代码>?你的意思是项目
?是的,foo应该是insert。我道歉。是的,包装应该是通用的。在我的实际实现中,它有两个通用参数:key和tree\u record。当我在主过程中实现它时,我应该传递一个键(名称)的字符串,tree_记录将成为一个带有名称和电话号码的记录,这两个都是字符串。另外,不,我不打算插入指针。作为参数传递的指针应该指向我希望插入新节点的树的根。