c++11 - What is the 'override' keyword in C++ used for? -
this question has answer here:
i beginner in c++. have come across override
keyword used in header file working on. may know, real use of override
, perhaps example easy understand.
the override
keyword serves 2 purposes:
- it shows reader of code "this virtual method, overriding virtual method of base class."
- the compiler knows it's override, can "check" not altering/adding new methods think overrides.
to explain latter:
class base { public: virtual int foo(float x) = 0; }; class derived: public base { public: int foo(float x) override { ... stuff x , such ... } } class derived2: public base { public: int foo(int x) override { ... } };
in derived2
compiler issue error "changing type". without override
, @ compiler give warning "you hiding virtual method same name".
Comments
Post a Comment