tobi
tobi

Reputation: 2012

Should interface/abstract class contain only pure virtual methods?

I am a bit weak with designing and I wonder whether it's a good design to have simple virtual methods (not only pure virtual) in an interface? I have a class that is some kind of interface:

class IModel {
    void initialize(...);
    void render(...);

    int getVertexCount() const;
    int getAnotherField() const;
};

the initialize and render methods need to be reimplemented for sure, so they are good candidates for pure virtual methods. However, the two last methods are very simple and practically always with the same implementation (just returning some field). Can I leave them as virtual methods with default implementation or is it better to have it pure virtual that needs to be reimplemented, because it's an interface?

Upvotes: 3

Views: 2451

Answers (3)

One thing you could do is make IModel be an interface and provide base class, eg ModelBase that implements common/repeating functionality.

class IModel
{
    virtual void initialize(...) = 0;
    virtual void render(...) = 0

    virtual int getVertexCount() const = 0;
    virtual int getAnotherField() const = 0;
};

class ModelBase : public IModel
{
    // common functions
    virtual int getVertexCount() const override { return vertexCount_; }
    virtual int getAnotherField() const override { return anotherField_; }

protected:
    int vertexCount_ = 0, anotherField_ = 0;
};

class MyModel : public ModelBase
{
    virtual void initialize(...) override { ... }
    virtual void render(...) override { ... }
};

The one downside of this approach is that there will be some (probably negligible) performance penalty due to extra virtual functions and loss of optimizations by the compiler.

Upvotes: 0

thiton
thiton

Reputation: 36059

You are facing a trade-off between code repetition and readability. The reader of your code will derive good help from every pure interface and from every non-overridden method. However, the default implementation wil be duplicated by every subclass. Whether or not you should provide a default implementation depends on the likelihood that the default implementation will change and will then need to be changed all over the place.

Without knowing these details, a hard yes-or-no answer cannot be given.

Upvotes: 1

Paolo Tagliapietra
Paolo Tagliapietra

Reputation: 186

We have to point out some differences:

there is no such thing as "some kind of Interface", is this class supposed to be an Interface or an Abstract Class?

If it's supposed to be an Interface then the answer is: all its methods must be pure virtual (no implementation) and it must not contain fields, not even one. The most you can (must, actually) do is, like jaunchopanza said, giving an empty body to the virtual destructor, thus allowing the derived classes to be destructed accordingly.

If, instead, it's supposed to be an Abstract Class then you're free to add the fields m_vertexCount and m_anotherField (I suppose) and implement getVertexCount() and ՝getAnotherField()՝ as you please. However, you should not name it IModel, because the I prefix should be used only for Interfaces.

Edit: I think I'm one of those "Believers" of which Bo Persson is talking about :)

Upvotes: 3

Related Questions