I always think of having to use pointers for polymorphism. Using the canonical example:
DrawEngine:render(Shape *shape)
{
shape->draw();
shape->visible(true);
}
And passing in pointer to various Shape derived classes. Does it work the same with references
DrawEngine:render(Shape &shape)
{
shape.draw();
shape.visible(true);
}
is it even valid to do
engine.render(myTriangle); // myTriangle instance of class derived from Shape
If this works are there any differences between the two cases? I tried to find info in Stroustrup but found nothing.
I reopened this cos I wanted to explore just a tad more.
so at least one difference is dynamic_cast. For me polymorphism includes the use of dynamic_cast
can I go
Rhomboid & r = dynamic_cast<Rhomboid &>(shape);
what happens if the cast fails. Is this any different?
Rhomboid * r = dynamic_cast<Rhomboid*>(&shape);