Ada 中的多类型继承
假设我有以下内容:
type blah is abstract tagged
record
element1 : integer;
end record;
type blah2 is abstract tagged
record
element2 : integer;
end record;
我希望我可以做这样的事情:
type blah3 is abstract new blah1 and blah 2 with null record;
所以理论上我现在可以访问 blah3.element1 和 blah3.element2
这可能吗?以及任何提示或技巧?
更新:
是否可以使用指针引用 blah3 (包含 blah 和 blah2)的元素?
IE(这只是一个粗略的想法,代码很糟糕......哈哈)
type blah3 is new type with
record
element1 : ptr to blah.element1;
element2 : ptr to blah2.element2;
end record
,然后可以通过 例如 blah3.element1 ?
Suppose I have the following:
type blah is abstract tagged
record
element1 : integer;
end record;
type blah2 is abstract tagged
record
element2 : integer;
end record;
I'm hoping that it's possible that I can do something like this:
type blah3 is abstract new blah1 and blah 2 with null record;
So in theory I can now access blah3.element1 and blah3.element2
Is this possible? and any hints or tips?
UPDATE:
Would it be possible to references the elements of blah3 (containing blah and blah2) using pointers?
I.E. (this is just a rough idea code is terrible...LOL)
type blah3 is new type with
record
element1 : ptr to blah.element1;
element2 : ptr to blah2.element2;
end record
and are then able to be accessed via
blah3.element1 for example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Marc C 是对的(像往常一样)。
即使在支持直接多重继承的语言中,直接多重继承也是非常有争议的。在某些边缘情况下,编译器应该做什么存在很大的问题,例如当两个父类定义同一方法或成员的不同版本时。当他们添加继承时,Ada95 中明确不允许允许这种情况。
所以你的下一个问题将是“那么我该如何做我想做的事呢?”
这取决于您想通过使用多重继承来实现什么目标。在最坏(最复杂)的情况下,您通常可以通过“mixin”继承实现您正在寻找的效果。我以前曾经做过,但我仍然认为这篇 AdaIC 文章对此进行了最好的解释:Ada95 和多重继承 比我自己做的还要多。
这是一个摘要:
看来 Ada 2005 有另一种更简单的方法来做到这一点(“接口”),但我还没有机会尝试。您可以阅读有关它的更多信息(包括为什么直接 MI 在 Ada 中仍然被认为是不好的)此处。我找到了这个例子。同样,只有当您的编译器支持 Ada 2005 时,这才有效
Marc C is right (as usual).
Direct Multiple inheritance is very controversial even in the languages that support it. There are big issues about what the compiler is supposed to do in some edge cases, like when both parent classes define different versions of the same method or member. It was explicitly not allowed in Ada95 when they added inheritance.
So your next question will be "So how do I do what i want to do?"
It depends on what you are trying to achieve by using multiple inheritance. In the worst (most complicated) case you can usually achieve the effect you are looking for with "mixin" inheritance. I have done it before, but still I think it is explained best in this AdaIC article: Ada95 and Multiple Inheritance than I could do myself.
Here's a digest:
It appears that Ada 2005 has another easier way to do this ("interfaces"), but I haven't had a chance to try that yet. You can read more about it (including why direct MI is still considered bad in Ada) here. I found this example. Again, this will only work if your compiler supports Ada 2005