Implementing a pure virtual function from an abstract base class: Does the overriding specifier make any sense?

Background

I just stumbled upon the option of using a overridespecifier , which, as far as I can tell, seems redundant, and without any specific semantics it means, but maybe something is missing for me, so this question. Before continuing, I must indicate that I tried to find the answer to it here on SO, but the closest I received the following streams without responding to my request (maybe someone can specify Q & A, which actually already answers on my question).

Question

Consider the following abstract class:

struct Abstract {
  virtual ~Abstract() {};
  virtual void foo() = 0;
};

override foo() - , Abstract ( DerivedB )? I.e., foo() , ( -)?

/* "common" derived class implementation, in my personal experience
   (include virtual keyword for semantics) */
struct DerivedA : public Abstract {
  virtual void foo() { std::cout << "A foo" << std::endl; }
};

/* is there any reason for having the override specifier here? */
struct DerivedB : public Abstract {
  virtual void foo() override { std::cout << "B foo" << std::endl; }
};
+4
4

override, , , , , , override , , . :

struct Base {
    virtual void f() = 0;
};

struct Derived : Base {
    virtual void f();
    virtual void f(int);
};

, Base (, ) Base :

struct Base {
    virtual void f(int) = 0;
};

Derived . override .

+8

. ( Pete)

, , , " " " "

, , .

override. .

, override:

,

class A
{
    virtual void Foo();
};

class B: public A
{
    virtual void Foo() override;
};

Foo const A. override , A->foo() B- B->foo(), - , - . override .

+2

. override . , -, override, . , - . :

class Abstract {
    virtual void foo() { ...}
}; 

class Derived : public Abstract {
    void foo() override { ... }
};

, Abstract::foo , ,

class Abstract {
    virtual void foo(int bar) { ...}
}; 

Derived::foo, Abstract, override. . (.. ) . override " ", . : http://en.cppreference.com/w/cpp/language/override

+2

override . :

class Foo
{
public: 
    virtual void foo() = 0;
};
class Derived : public Foo
{
public:
    //....
    virtual void foo(double x) override
    {
         //This throws error
    }
};

As you can see above, the compiler throws an error if you compiled above. What will happen, the compiler will complain about a function that does not have the same signature. Without a keyword, the overrideresult will be different.

+1
source

Source: https://habr.com/ru/post/1653908/


All Articles