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:

  1. it shows reader of code "this virtual method, overriding virtual method of base class."
  2. 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

Popular posts from this blog

vb.net - Alternative to the T-SQL AS keyword -

php - MySQLi binding parameters in a prepared statement doesn't work unless inserted after "WHERE" -

ios - CFRelease causing crash in iPad application -