C++ "virtual" keyword for functions in derived classes. Is it necessary?

Anarki picture Anarki · Feb 4, 2011 · Viewed 78.9k times · Source

With the struct definition given below...

struct A {
    virtual void hello() = 0;
};

Approach #1:

struct B : public A {
    virtual void hello() { ... }
};

Approach #2:

struct B : public A {
    void hello() { ... }
};

Is there any difference between these two ways to override the hello function?

Answer

James McNellis picture James McNellis · Feb 4, 2011

They are exactly the same. There is no difference between them other than that the first approach requires more typing and is potentially clearer.