Junior — Middle
What is the purpose of using the override keyword in programming?
sobes.tech AI
Answer from AI
The keyword override in C++ is used to explicitly indicate that a method in a derived class overrides a virtual method in the base class. It helps the compiler verify that the method indeed overrides an existing virtual method and avoid errors, such as typos in the name or signature mismatch.
Example:
struct Base {
virtual void foo();
};
struct Derived : Base {
void foo() override; // the compiler will check that foo() overrides a method from Base
};
If a method with override does not match any virtual method in the base class, the compiler will generate an error.