David
David

Reputation: 28178

Is override for the user of a class or for the implementer?

This is basically a best practice question, and a question about the intended usage of override:

struct A
{
    virtual void func();
};

struct B : public A
{
    virtual void func() override;
};

In the above case it seems totally reasonable to use override. It tells the user of B that func overrides a virtual function from A (information that could only be conveyed with a comment pre-C++11). But what about when the user isn't supposed to perceive that A is a base class... like if A is privately inherited:

struct B : private A
{
    virtual void func() override;
};

In this case I'm telling the user of B about an internal implementation detail they shouldn't know exists. If the purpose of override is to give the writer of B an error if he didn't actually override anything, that's fine; But if the purpose is to convey more information to the user, should I not write override in this case?

Upvotes: 0

Views: 159

Answers (1)

Christian Stieber
Christian Stieber

Reputation: 12496

"override" is simply there so the implementor of a class can say "this method is supposed to override one from my base classes". If he does, then the compiler will provide an error if the method does not actually override a base class method -- which can happen if you mistype the name, or the base class changes later.

EDIT: Thus, it's a tool that helps with writing correct code; it's not meant to be a documentation keyword of some kind.

Upvotes: 2

Related Questions