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?
C
if it's abstract. On the other hand, I would say overwriting and overwriten function is not a good practice, in which case would you use this? – Cavanoverride
pure virtual functions like it is done here instruct B
and found that this seems indeed good practice - see #46447152 – Levirate