Paul Manta
Paul Manta

Reputation: 31567

Make implementation of virtuals also virtual?

When implementing a pure virtual function in C++, is there a best-practices guideline that says the implementation should also be made virtual? What is the rationale?

class Interface
{
  public:
    virtual void foobar() = 0;
};

class Concrete
    : public Interface
{
  public:
    virtual void foobar();
};

Upvotes: 4

Views: 143

Answers (2)

Johann Gerell
Johann Gerell

Reputation: 25581

Although it doesn't matter if the virtual keyword is present in a derived class or not, I've found it to be an indispensable time-saving self-documenting practice to always include it, so that anyone working with your code a two years from now immediately can see that there is more to the class than what immediately meets the eye.

Upvotes: 7

James McNellis
James McNellis

Reputation: 355039

It does not matter.

void foobar() in Concrete is virtual regardless whether you declare it as such and it overrides the void foobar() in Interface.

Upvotes: 13

Related Questions