Kem*_*mal 7 c++ multiple-inheritance language-lawyer
请考虑以下代码:
#include <iostream>
#include <string>
using namespace std;
// helpers
void disp1(string s, int i, void* p) { cout << s << " constructed with " << i << " @ " << p << "\n"; }
void disp2(string s, void* p) { cout << s << " @ " << p << "\n"; }
// class hierarchy:
//
// A A
// | |
// B C
// \ /
// D
//
struct A { A() { disp1("A", 0, this); } void tell() { disp2("A", this); } };
struct B : A { B() { disp1("B", 0, this); } void tell() { disp2("B", this); } };
struct C : A { C() { disp1("C", 0, this); } void tell() { disp2("C", this); } };
struct D : B, C { D() { disp1("D", 0, this); } void tell() { disp2("D", this); } };
int main()
{
D d;
static_cast<B*>(&d)->A::tell(); // OK: call B::A::tell()
static_cast<C*>(&d)->A::tell(); // OK: call C::A::tell()
// d.B::A::tell(); // compile error: 'A' is an ambiguous base of 'D'
}
Run Code Online (Sandbox Code Playgroud)
为什么我在合格的调用上遇到编译器错误d.B::A::tell();?确实,这A是一个模糊的基础D,但为什么它在这里相关?
我明确地说:"通话tell()中A的B".有什么不明白的?