Is it valid to override virtual function with pure specifier?

Yksisarvinen :

Note: I do not ask whether or not this is reasonable thing to do or if this is good design. I'm just asking if this is well-defined behaviour and if the results are as expected.

I came upon a following class hierarchy:

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

struct B: public A
{
    void foo() override 
    {
        std::cout << "B::foo()\n";
    }
};

struct C: public B
{
    virtual void foo() = 0;
};

struct D: public C
{
    void foo() override
    {
        std::cout << "D::foo()\n";
    }
};

int main()
{
    A* d = new D;
    d->foo(); //outputs "D::foo()"
    // A* c = new C; // doesn't compile as expected
}

Is this code well defined? Are we allowed to override definition with pure-specifier?

Daniel Langr :

[class.abstract/5] of the current draft Standard:

[Note: An abstract class can be derived from a class that is not abstract, and a pure virtual function may override a virtual function which is not pure. — end note]

The very same note is included even in the C++11 Standard. So, the answer is yes, it is valid.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29347&siteId=1