7

Consider the code below:

#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'

}

Why do I get compiler error on a qualified call such as d.B::A::tell();? It is true that A is an ambiguous base of D, but why is it relevant here?

I am explicitly saying "call tell() of A of B". What is ambiguous about this?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Kemal
  • 849
  • 5
  • 21

0 Answers0