Let’s say we have following code:
struct A{
virtual ~A(){}
void f(){
p = 42;
}
int p;
};
struct B : public virtual A{};
struct C : public virtual A{};
struct D : public B, public C, public A{}; //let's add non-virtual inheritance
int main(){
D* pA = new D();
pA->A::f(); //!
return 0;
}
Is there any way to set p to 42 in the most base class A?
Following construction pA->A::f(); sets p to 42 for non-virtual inherited class A. Can we do that without cast?
First off, there is no cast: you just qualify which version of
Ayou want as there are more than one. Of course, the notation you have chosen actually doesn’t work because it doesn’t resolve the ambiguity in the first place. I guess you meant to use something likeIf you don’t want to put the burden of choosing which member function to call on the user of your class, you’ll have to provide suitable forwarding functions for
De.g.: