Suppose I have something like the following:
class Point : geometry {
...
Point(double x, double y) {
}
double distanceTo(Line) {
}
double distanceTo(Point) {
}
}
class Line : geometry {
...
Line(double x, double y, double slopex, double slopey) {
}
double distanceTo(Line) {
}
double distanceTo(Point) {
}
}
struct point_t {
double x, y;
}
struct line_t {
double x, y, slope_x, slope_y;
}
struct Geom_Object_t {
int type;
union {
point_t p;
line_t l;
} geom;
}
I am wondering what the best way to define a dispatch table for a function like
double distanceTo(Geom_Object_t * geom1, Geom_Object_t * geom2) {
}
The classes are written in C++, but the distanceTo function and the struct must be externed to C
thanks
I would make the class diagram different: an abstract base class
GeomObject, subclassinggeometry(with agetTypeaccessor, as well as pure virtualdistanceTooverloads), and concrete subclassesLineandPointofGeomObject(with overrides of the accessor and overloads). The need to"extern C"thedouble distanceTofunction is not a problem, since you're not talking about overloads of that function anyway: you simply want to returngeom1.distanceTo(x)(letting the virtual table do that part of the work;-) wherexis an appropriate cast, e.g., assuming the class diagram I've explained: