確切的說override並不是一個keywordthis
The override
keyword serves two purposes:spa
To explain the latter:code
class base { public: virtual int foo(float x) = 0; }; class derived: public base { public: int foo(float x) override { ... do stuff with x and such ... } } class derived2: public base { public: int foo(int x) override { ... } };
In derived2
the compiler will issue an error for "changing the type". Without override
, at most the compiler would give a warning for "you are hiding virtual method by same name".blog