V
Victor Bogado
I want to create a class hierarchy for geometric elements, each
element must know how to report if he intersect with other element.
The interface is the following :
class Element
{
public:
...
// Here we could have a naive implementation
// (using bounding boxes for instance)
bool intersect(Element &e)
{
...
}
...
};
class SquareElement : public Element
{
public:
bool intersect(SquareElement &e)
{
...
}
};
class CircleElement
{
public:
bool intersect(SquareElement &e)
{
...
}
bool intersect(CircleElement &e)
{
...
}
};
The problem is that when I execute squareRef.intersect(circleRef) the C
++ will call the Element::intersect(Element &) because it don't do
dynamic binding on the parameters. So I thought on the scheme :
class Element
class Element
{
public:
...
// Here we could have a naive implementation
// (using bounding boxes for instance)
bool intersect(Element &e)
{
...
}
...
};
class SquareElement : public Element
{
public:
bool intersect(Element &e)
{
return e.intersect(*this);
}
protected:
bool doIntersect(SquareElement &e)
{
...
}
};
class CircleElement
{
public:
bool intersect(Element &e)
{
return e.intersect(*this);
}
protected:
bool intersect(SquareElement &e)
{
...
}
bool intersect(CircleElement &e)
{
...
}
};
this solves some problems, but not all of them, unfortunately all
classes must know about all other classes in this case, otherwise the
compiler will use the generic form. Also there is a unbalance where
squareRef.intersect(circleRef) is potentially different from
circleRef.intersect(squareRef).
element must know how to report if he intersect with other element.
The interface is the following :
class Element
{
public:
...
// Here we could have a naive implementation
// (using bounding boxes for instance)
bool intersect(Element &e)
{
...
}
...
};
class SquareElement : public Element
{
public:
bool intersect(SquareElement &e)
{
...
}
};
class CircleElement
{
public:
bool intersect(SquareElement &e)
{
...
}
bool intersect(CircleElement &e)
{
...
}
};
The problem is that when I execute squareRef.intersect(circleRef) the C
++ will call the Element::intersect(Element &) because it don't do
dynamic binding on the parameters. So I thought on the scheme :
class Element
class Element
{
public:
...
// Here we could have a naive implementation
// (using bounding boxes for instance)
bool intersect(Element &e)
{
...
}
...
};
class SquareElement : public Element
{
public:
bool intersect(Element &e)
{
return e.intersect(*this);
}
protected:
bool doIntersect(SquareElement &e)
{
...
}
};
class CircleElement
{
public:
bool intersect(Element &e)
{
return e.intersect(*this);
}
protected:
bool intersect(SquareElement &e)
{
...
}
bool intersect(CircleElement &e)
{
...
}
};
this solves some problems, but not all of them, unfortunately all
classes must know about all other classes in this case, otherwise the
compiler will use the generic form. Also there is a unbalance where
squareRef.intersect(circleRef) is potentially different from
circleRef.intersect(squareRef).