多重继承是编译器编写者的问题吗? - c++

发布于 2024-08-27 03:52:10 字数 652 浏览 3 评论 0原文

我一直在阅读有关多重继承的内容

多重继承的确切问题是什么? http://en.wikipedia.org/wiki/Diamond_problem

http://en.wikipedia.org/wiki/Virtual_inheritance
http://en.wikipedia.org/wiki/Multiple_inheritance

但是由于代码无法编译在歧义得到解决之前,这是否只会使多重继承成为编译器编写者的问题? - 如果我不想编写编译器,这个问题对我有何影响

i have been reading about multiple inheritance

What is the exact problem with multiple inheritance?
http://en.wikipedia.org/wiki/Diamond_problem

http://en.wikipedia.org/wiki/Virtual_inheritance
http://en.wikipedia.org/wiki/Multiple_inheritance

But since the code does not compile until the ambiguity is resolved, doesn't this make multiple inheritance a problem for compiler writers only? - how does this problem affect me in case i don't want to ever code a compiler

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

雨轻弹 2024-09-03 03:52:10

如果你想编写可以编译的代码,你必须知道什么样的问题可能会导致它无法编译以及如何避免这些情况。作为编译器的用户,您的问题是如何以可编译的方式设计您的继承层次结构。

此外,如果您不了解多重继承是如何工作的,您可能会对您的类到底做什么有错误的假设。如果类的行为与您期望的不同,那么当您尝试使用它们时就会导致错误。

If you want to write code that compiles, you have to know what kind of problems might cause it to not compile and how to avoid these situations. It is your problem, as a user of the compiler, to design your inheritance hierarchies in a way that they will be compilable.

Also if you don't understand how multiple inheritance works you might have wrong assumptions about what exactly your classes do. If classes behave differently than you expect it will cause bugs when you try to use them.

一张白纸 2024-09-03 03:52:10

如果由于多重继承而存在未解决的歧义,编译器编写器将打印一条令人讨厌的错误消息并停止编译您的代码。当他们说在解决歧义之前代码无法编译时,您需要考虑几个问题:

  1. 在解决歧义之前,您没有一个可用的程序。
  2. 编译器不会为你解决它。
  3. 因此,在您解决它之前,这是您的问题,而不是编译器编写者的问题。

The compiler writer will print a nasty error message and stop compiling your code if you have an unresolved ambiguity due to multiple inheritance. When they say that the code won't compile until the ambiguity is resolved, there are several issues for you to consider:

  1. You don't have a working program until the ambiguity is resolved.
  2. The compiler doesn't resolve it for you.
  3. Therefore, until you resolve it, it's your problem, not the compiler writer's.
满栀 2024-09-03 03:52:10

不,这对于编译器编写者来说不是问题:

  • 一般来说,编译器编写者可能会定义多重继承的工作方式。
  • 具体针对C++,有几种解决方案可供作者实现。

对于 C++ 程序员来说,这是一个问题,但前提是您不了解 MI 在 C++ 中的工作原理。

我有一个通用的解决方案,那就是拥有一个定义公共接口的基类 - 然后您将其视为具有不同的子部分,然后将其实现为不同的抽象类,这些抽象类由具体的叶类多重继承

         ------
        | Base |
         ------
        |      |
         ------
           ^
           |
     -----------------
    |        |        |
 ------   ------   ------ 
|  A   | |  B   | |  C   |
 ------   ------   ------ 
|      | |      | |      |
 ------   ------   ------ 
    ^        ^        ^
    |        |        |
     -----------------
            |
          -------
         |Derived|
          -------
         |       |
          -------

: 、B 和 C 实现 Base 的非重叠子部分,这意味着您可以将 A 替换为 A',以获得替代或改进的实现,而不会影响任何其他类。

No, its not a problem for a compiler writer:

  • In general a compiler writer might be defining how multiple inheritance works.
  • Specifically for C++, there are several solutions for the writer to implement.

It is a problem for the C++ programmer, but only if you don't understand how MI works in C++.

I have one general solution which is to have a base class which defines a public interface - which you then think of as having distinct subsections, which you then implement as distinct abstract classes, which are multiply inherited by a concrete leaf class:

         ------
        | Base |
         ------
        |      |
         ------
           ^
           |
     -----------------
    |        |        |
 ------   ------   ------ 
|  A   | |  B   | |  C   |
 ------   ------   ------ 
|      | |      | |      |
 ------   ------   ------ 
    ^        ^        ^
    |        |        |
     -----------------
            |
          -------
         |Derived|
          -------
         |       |
          -------

Each of A, B and C implement non-overlapping subsections of Base, which means you can swap out, say A, for A' for an alternate or improved implementation without affecting any other class.

日记撕了你也走了 2024-09-03 03:52:10

总的来说,是的,你说得一中要害。它显着增加了维护编译器的复杂性和工作量,但对于程序员来说,它只增加了少量的额外复杂性,主要与出现钻石问题时必须进行繁琐的具体操作有关。 (这在设计良好的对象层次结构中应该很少见。)

In general, yes, you hit the nail on the head. It significantly increases the complexity and effort involved in maintaining the compiler, but for the programmer, it only adds a small amount of additional complexity, mostly related to having to be tediously specific if a diamond problem arises. (Which should be very rare in a well-designed object hierarchy.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文