Implementing the same method signature from two 'interfaces'
Asked Answered
U

2

18

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".

Utterance answered 24/10, 2011 at 8:27 Comment(2)
Did you try it? What is the behavior you see?Statued
"Does the fact Iaaa and Ibbb have no method body make things easier/better" It never does. Why would it?Venison
S
16

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 classes with all pure virtual functions) methods.

Also, 2 other points from your question

  1. diamond inheritance hasn't yet took place, because you haven't mentioned any common base for Iaaa and Ibbb
  2. You cannot implement, 2 X::setVisible(bool) with same signature
Syncretism answered 24/10, 2011 at 8:31 Comment(1)
"well defined" is the answer I was after - it sounds like there is no problem here.Utterance
E
10

It 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.)

Earwitness answered 24/10, 2011 at 9:21 Comment(1)
a)I use "faux-interface" because interfaces are not a language construct in C++... not meaning to imply they aren't 'real' interfaces. An issue of technicality/nonclementure.... b)I this case I would want the same implementation for both. But it is worth mentioning.Utterance

© 2022 - 2024 — McMap. All rights reserved.