Using pure virtual methods for faux-interfaces in C++, what happens when a concrete class derives from two 'interfaces' which have an identical method declaration? e.g X
sub-classes Iaaa
and Ibbb
and implements a method virtual void setVisible(bool);
.
Does the fact Iaaa
and Ibbb
have no method body make things easier/better than a more classical diamond inheritance scenario, and lets X::setVisible
be the implementation for both Iaaa::setVisible
and Ibbb::setVisible
?
I suppose a more C++ way of phrasing the question is "what happens when one class uses MI to derive from 2 classes which have identical signatures for a pure virtual method".
No, its an errorIf two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.
A class implementation of a method takes precedence over a default method. So, if the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect. However, if two interfaces implement the same default method, then there is a conflict.
Question 2: What to do if a class implements two interfaces which coincidently have one method with the same name and signature? Implement both interfaces implicitly. Implement at least one interface explicitly. A class cannot implement multiple interfaces.
Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.
what happens when one class uses MI to derive from 2 classes which have identical signatures for a pure virtual method
The behavior is well defined. The derived class overriding method will constitute implementation of both the interfaces (i.e. abstract class
es with all pure virtual
functions) methods.
Also, 2 other points from your question
Iaaa
and Ibbb
X::setVisible(bool)
with same signatureIt depends. If both functions have the same signature, and you want to replace both of them with the same function, then just do it; there is no problem, and your function will implement both of them. If they have different signatures, then you'll need two different functions to implement them. And if you want different implementations (likely the case if the interfaces are unrelated), but they have the same signature, then you'll need to introduce intermediate classes to "rename" them, e.g.:
class MaskSetVisibleInAaa : public Aaa
{
virtual void setVisibleInAaa( bool ) = 0;
virtual void setVisible( bool newStatus )
{
setVisibleInAaa( newStatus );
}
};
class MaskSetVisibleInBbb : public Bbb
{
virtual void setVisibleInBbb( bool ) = 0;
virtual void setVisible( bool newStatus )
{
setVisibleInBbb( newStatus );
}
};
class ConcreteImplementation
: public MaskSetVisibleInAaa
, public MaskSetVisibleInBbb
{
virtual void setVisibleInAaa( bool );
virtual void setVisibleInBbb( bool );
};
(I would also question your use of "faux-interface". C++ fully supports
real interfaces—more so, in fact, than some other languages which
have an interface
keyword. An interface defines a contract; which,
unless the language has special support for programming by contract,
implies in most cases concrete code in the interface, with virtual
functions being private.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With