I'm using double dispatch to obtain the distance between 2 objects of 2 classes(B
, C
) that are subclasses of another (A).
I think that the methods in class A
should be pure virtual, but them are used in a test elsewhere, so the class A
must be instantiable, so I can't make them pure virtual, right?
Another thing is that I'm not sure if I'm using double dispatch in a good way, since sometimes generates a infinite loop, in the definition of Distance(A *a)
in the base class.
In the class A
, I have the methods:
virtual double Distance(A *a) {return a->Distance(this);}
virtual double DistanceB(B *b) {return std::numeric_limits<double>::max();}
virtual double DistanceB(C *c) {return std::numeric_limits<double>::max();}
virtual double DistanceC(B *b) {return std::numeric_limits<double>::max();}
virtual double DistanceC(C *c) {return std::numeric_limits<double>::max();}
In the class B
:
double B::Distance(A *a) { return a->DistanceB(this); }
double B::DistanceB(B *b) { /*calculate distance*/ }
double B::DistanceC(C *c) { return c->DistanceB(this); }
In the class C
:
double C::Distance(A *a) { return a->DistanceC(this); }
double C::DistanceB(B *b) { /*calculate distance*/ }
double C::DistanceC(C *c) { /*calculate distance*/ }