Ada 中的多类型继承

发布于 2024-08-23 11:34:06 字数 643 浏览 9 评论 0原文

假设我有以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花期渐远 2024-08-30 11:34:06

Marc C 是对的(像往常一样)。

即使在支持直接多重继承的语言中,直接多重继承也是非常有争议的。在某些边缘情况下,编译器应该做什么存在很大的问题,例如当两个父类定义同一方法或成员的不同版本时。当他们添加继承时,Ada95 中明确不允许允许这种情况。

所以你的下一个问题将是“那么我该如何做我想做的事呢?”

这取决于您想通过使用多重继承来实现什么目标。在最坏(最复杂)的情况下,您通常可以通过“mixin”继承实现您正在寻找的效果。我以前曾经做过,但我仍然认为这篇 AdaIC 文章对此进行了最好的解释:Ada95 和多重继承 比我自己做的还要多。

这是一个摘要:

Ada 95支持多重继承
模块包含(通过多个
“with”/“use”子句),
多重继承
通过私有“is-implement-using”
扩展和记录组成,以及
通过多重继承混合
使用泛型、正式包,以及
访问判别器。

看来 Ada 2005 有另一种更简单的方法来做到这一点(“接口”),但我还没有机会尝试。您可以阅读有关它的更多信息(包括为什么直接 MI 在 Ada 中仍然被认为是不好的)此处。我找到了这个例子。同样,只有当您的编译器支持 Ada 2005 时,这才有效

Interfaces can be composed from other interfaces thus 
type Int2 is interface;
...
type Int3 is interface and Int1;
...
type Int4 is interface and Int1 and Int2;
...

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:

Ada 95 supports multiple-inheritance
module inclusion (via multiple
"with"/"use" clauses),
multiple-inheritance
"is-implemented-using" via private
extensions and record composition, and
multiple-inheritance mix-ins via the
use of generics, formal packages, and
access discriminants.

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

Interfaces can be composed from other interfaces thus 
type Int2 is interface;
...
type Int3 is interface and Int1;
...
type Int4 is interface and Int1 and Int2;
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文