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();
};
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
.
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.
精彩评论