Where to use the override keyword in C++

  • A+
Category:Languages

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; } 

 


You can't put an override specifier when defining the function. The language doesn't allow it, and a compiler will complain. So there's only really 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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: