I want to create a program that has an "element" class, the element is
a graphic object that has a finite shape. It goes some what like
this :
class Element
{
public :
// Returns the minimum radius that encompass the holle element
double radius() = 0;
void center(double &x, double &y) = 0;
double distance(Element &e) = 0;
// naive implementation
bool intersect(Element &e)
{
return distance(*this) < radius() + e.radius();
}
};
Now suppose I have a decedent class that holds a "circle element" and
another that holds a "square element", how do I make a smarter
intersect that compares both of them? If I simply create a "bool
intersect(CircleElement &e)" in the SquareElement class it will not be
called if I use this code :
Element *a = new CircleElement(...);
Element *b = new SquareElement(...);
if (b->intersect(a)) {...}
Because C++ does not make the dynamic binding on the parameters, only
on the class. So the best I can do is to compare a SquareElement to a
generic element. Is there a way out? How this is done usually?