多重继承是编译器编写者的问题吗? - c++
我一直在阅读有关多重继承的内容
多重继承的确切问题是什么? 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想编写可以编译的代码,你必须知道什么样的问题可能会导致它无法编译以及如何避免这些情况。作为编译器的用户,您的问题是如何以可编译的方式设计您的继承层次结构。
此外,如果您不了解多重继承是如何工作的,您可能会对您的类到底做什么有错误的假设。如果类的行为与您期望的不同,那么当您尝试使用它们时就会导致错误。
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.
如果由于多重继承而存在未解决的歧义,编译器编写器将打印一条令人讨厌的错误消息并停止编译您的代码。当他们说在解决歧义之前代码无法编译时,您需要考虑几个问题:
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:
不,这对于编译器编写者来说不是问题:
对于 C++ 程序员来说,这是一个问题,但前提是您不了解 MI 在 C++ 中的工作原理。
我有一个通用的解决方案,那就是拥有一个定义公共接口的基类 - 然后您将其视为具有不同的子部分,然后将其实现为不同的抽象类,这些抽象类由具体的叶类多重继承
: 、B 和 C 实现 Base 的非重叠子部分,这意味着您可以将 A 替换为 A',以获得替代或改进的实现,而不会影响任何其他类。
No, its not a problem for a compiler writer:
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:
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.
总的来说,是的,你说得一中要害。它显着增加了维护编译器的复杂性和工作量,但对于程序员来说,它只增加了少量的额外复杂性,主要与出现钻石问题时必须进行繁琐的具体操作有关。 (这在设计良好的对象层次结构中应该很少见。)
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.)