Does the Non-Virtual Interface idiom not apply to pure virtual functions?

I used to write code like this:

class SomeInterface
{
public:
    virtual void doThings() = 0;
    virtual void run() = 0;
};

class OtherInterface
{
public:
    virtual void doStuff() = 0;
    virtual void run() = 0;
};

class ConcreteClass : public SomeInterface, public OtherInterface
{
public:
    virtual void doThings() { ... }
    virtual void doStuff() { ... }
    virtual void run() { ... }
};

But I read recently about the Non-Virtual Interface idiom (stuff like this http://www.gotw.ca/publications/mill18.htm), and following those guidelines I should instead write my interfaces like this:

class SomeInterface
{
public:
    void doThings() { doThingsImplementation(); }
    void run() { runImplementation(); }
private:
    virtual void doThingsImplementation() = 0;
    virtual void runImplementation() = 0;
};

class OtherInterface
{
public:
    void doStuff() { doStuffImplementation(); }
    void run() { runImplementation(); }
private:
    virtual void doStuffImplementation() = 0;
    virtual void runImplementation() = 0;
};

…except that in this example the harmless overlap in the interfaces becomes a diamond inheritance issue.

Rereading the argument for the Non-Virtual Interface idiom, I get the feeling that the issue is because I’m applying it to virtual functions that are pure virtual functions. Is that it? Should I only use the NVI idiom for non-pure virtual functions?

Source: Windows Questions C++

LEAVE A COMMENT