@ Purple Giraffe
This is a response to the answer of the Purple Giraffe. It also eliminates some of the misunderstandings of late binding and MI, which are more flexible than Java inheritance.
therefore it doesnβt matter if you call it on I1 * or I2 * as long as the real object is IImpl.
// interfaces class I1 { public: virtual void f () = 0; }; class I2 { public: virtual void f () = 0; }; // concrete implementations class C1 : public I1 { public: virtual void f () { cout << "C1::f()\n"; } }; class C2 : public I2 { public: virtual void f () { cout << "C2::f()\n"; } }; // putting both together class D : public C1, public C2 { }; template <class I> void f (I &i) { if(); // virtual call to I::f() } int main() { D d; f<I1> (d); // virtual call to I1::f(): prints C1::f() f<I2> (d); // virtual call to I2::f(): prints C2::f() }
The call of a member function is always statically associated with the declaration of one function (here I1::f() and I2::f() ). At run time, a function called (by the "late binding" method) is the most derived handler (the technical term is "final") of this virtual function (here C1::f() and C2::f() ).
In any class there should be no more than one final redefinition of each pure virtual function. In a non-abstract class, there must be exactly one final redirector for each virtual function.
Now you see that I1::f() and I2::f() are different functions and that they are completely unrelated; but both have the same unqualified name and the same parameters, so they cannot be distinguished when overloaded. And if a qualified name is used ( d.I1::f() and d.I2::f() ), the call will be bound to the correct declaration, but virtuality will also be blocked (in this case, the definition of a pure virtual function must exist and will be called ) So the only way to call any function is what I did here: first make an lvalue of type I1 / I2 , and then make an unqualified call to .f() .
Side note. Such an explicit redefinition is not possible in Java, because MI is strictly limited, and redefinition follows different rules.
In addition, IImpl has two similar R () that you expect to call for impl.R ()?
What f() do you expect to call in df() ?
source share