4 ms·
Take this scenario: Rectangle *r = new Rectangle(); r->draw(); // calls Rectangle::draw(GREEN) Shape *s = *r; s->draw(): // calls Rectangle::draw(RED)
by Sebb767 2y ago
Take this scenario:
Rectangle *r = new Rectangle();
r->draw(); // calls Rectangle::draw(GREEN)
Shape *s = *r;
s->draw(): // calls Rectangle::draw(RED)
Intuitively, you would not expect that a function changes its behavior when it's called from a pointer of a different type. I can easily see this leading to very a fun-to-reproduce-kind of bug.
- cryptonector 2y agoOTOH if you don't know what the type of `s` run-time type is then you might draw comfort from knowing that the parameter's default is that of `s` static type.