Where to use the override keyword in C++

Viewed 13328

Which is an ideal file to use override identifier in c++ .h or .cpp?

war.h

class TimeStone : public InfinityStone
{
private:
    bool capturedByThanos() override; #1
};

war.cpp

bool TimeStone::capturedByThanos() override #2
{
    return true;
}
1 Answers

You can't put an override specifier when defining the function outside the class's member specification. The language doesn't allow it, and a compiler will complain. So there's really only one option.

Beyond that, this option also makes more sense. When declaring the function you are expressing an intent to override it. So putting override there at the point of expressing your intent makes sense. That is what you are asking the compiler to check here, your intent to override. The declaration is also enough to verify that function is originally declared virtual and that you got the signature right.

Related