Diamond Problem

前端 未结 3 1101
有刺的猬
有刺的猬 2020-11-30 06:01

Wikipedia on the diamond problem:

\"... the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and

3条回答
  •  时光说笑
    2020-11-30 06:28

    In the diamond problem, class D implicitly inherits the virtual method from class A. To call it, class D would call:

    A::foo()
    

    If both classes B and C override this method, then the problem comes of which actually gets called.

    In your second example however, this isn't the case as class D would need to explicitly state which was being called:

    B::foo()
    C::foo()
    

    So the problems are not actually the same. In the diamond problem you aren't referencing the derived classes, but their base class, hence the ambiguity.

    That's how I understand it, anyway.

    Note that I'm coming from a C++ background.

提交回复
热议问题