Virtual Inheritance of a C ++ Virtual Method

struct A {
    virtual void foo() { std::cout << "a";};
};

struct B:public virtual A {
    void foo() { std::cout << "b";}
};

struct C:public virtual A {
    void foo() { std::cout << "c";}
};

struct D:public B, public C {

};
int main() {
    return 0;
}

So this one when compiling gives us the following error:

 \main.cpp:16:8: error: no unique final overrider for 'virtual void A::foo()' in 'D'
 struct D:public B, public C {

If we inherited the non-virtual structures B and C, the code compiles immediately without errors (but, of course, an error occurs if we call dd.foo ()). So what's the difference? Why do we have a mistake when we inherit our class practically and without errors, if we do it directly?

+4
source share
1 answer

A B C , D A [1]. B C foo [2] D [2] D foo, [2].

A , B C, D A [1] foo [2].


[1]: N4140 ยง10.1 [ .mi]/4:

, virtual, . , virtual, . , . , , .

[2]: ยง10.3 [class.virtual]/2 ( ):

- vf โ€‹โ€‹ Base Derived, Base, - vf , - , cv-qualification ref- ( ), Base:: vf, Derived:: vf ( , ), Base:: vf.. , . - C:: vf S , , S ( ), -, vf. , - , .

+4

All Articles