This is multiple inheritance, and there are two inherited functions with the same signature, both of which have implementation. That's where C++ is different from Java.
Calling write
on an expression whose static type is MyBigClass
would therefore be ambiguous as to which of the inherited functions was desired.
If write
is only called through base class pointers, then defining write
in the derived class is NOT necessary, contrary to the claim in the question. Now that the question changed to include a pure specifier, implementing that function in the derived class is necessary to make the class concrete and instantiable.
MyWriter::write
cannot be used for the virtual call mechanism of MyBigClass
, because the virtual call mechanism requires a function that accepts an implicit IWriter* const this
, and MyWriter::write
accepts an implicit MyWriter* const this
. A new function is required, which must take into account the address difference between the IWriter
subobject and the MyWriter
subobject.
It would be theoretically possible for the compiler to create this new function automatically, but it would be fragile, since a change in a base class could suddenly cause a new function to be chosen for forwarding. It's less fragile in Java, where only single inheritance is possible (there's only one choice for what function to forward to), but in C++, which supports full multiple inheritance, the choice is ambiguous, and we haven't even started on diamond inheritance or virtual inheritance yet.
Actually, this problem (difference between subobject addresses) is solved for virtual inheritance. But it requires additional overhead that's not necessary most of the time, and a C++ guiding principle is "you don't pay for what you don't use".
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…