AlwaysLearning
AlwaysLearning

Reputation: 8041

Two base sub-objects vs. ambiguity

The following code comes from this post:

struct Base
{
    Base( int ) {}
    void foo() const {}
};

struct Intermediate: Base
{
    Intermediate( int x )
        : Base( x )
    {}
};

struct ResolvableBase: Base
{
    ResolvableBase( int x ): Base( x ) {}
};

struct Derived: Intermediate, ResolvableBase
{
    Derived( int x )
        : Intermediate( x )
        , ResolvableBase( x )
    {}
};

int main()
{
    Derived o( 667 );
    o.ResolvableBase::foo();    // OK.
} 

The author of this code seems to claim that o has two sub-objects of Base. Why is this the case of two sub-objects and not the case of ambiguity (in which case gcc would have warned about inaccessible base class)? Also, if there are two sub-objects, then which sub-objects's foo gets called in main?

Upvotes: 2

Views: 49

Answers (1)

Revolver_Ocelot
Revolver_Ocelot

Reputation: 8785

Why is this the case of two sub-objects and not the case of ambiguity

Because there is two distinct subobjects we can address separately if we want.

if there are two sub-objects, then which sub-objects's foo gets called in main?

One belonging to ResolvableBase, as you explicitely requested that one:

o.ResolvableBase::foo();    // OK.
  ~~~~~~~~~~~~~~ ← Here

Upvotes: 2

Related Questions