Template Dispatcher question
Nov 3, 2011 at 8:35pm UTC
FIXED!! Now It's not unsafe (see 1st comment).
The code below completely avoid rtti but it is a little bit difficult to maintain.
Is there a template way to eliminate group of virtual functions in GeometryPlace?
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
struct GeometryPlace
{
virtual const GeometryPlace *intersect(const GeometryPlace *gp) const = 0;
.........
virtual const GeometryPlace *intersect(const class CircleArc2 *gp) const {}
virtual const GeometryPlace *intersect(const class LineSegment2 *gp) const {}
virtual const GeometryPlace *intersect(const class SemiPlan2 *gp) const {}
virtual const GeometryPlace *intersect(const class Point3 *gp) const {}
virtual const GeometryPlace *intersect(const class PlaceUnion *gp) const {}
.........
static const class GeometryPlace *intersect(const class CircleArc2 *c1, const class CircleArc2 *c2);
.........
.........
};
template <typename T>
struct IntersectDispatch : public GeometryPlace
{
virtual const GeometryPlace *intersect(const GeometryPlace *gp) const
{ return gp->intersect(static_cast <const T*>(this )); }
virtual const GeometryPlace *intersect(const class CircleArc2 *gp) const
{ return GeometryPlace::intersect(static_cast <const T*>(this ), gp); }
.........
virtual const GeometryPlace *intersect(const class PlaceUnion *gp) const
{ return GeometryPlace::intersect(static_cast <const T*>(this ), gp); }
};
struct CircleArc2 : public IntersectDispatch<CircleArc2> {}
.........
Last edited on Nov 3, 2011 at 11:31pm UTC
Nov 3, 2011 at 10:21pm UTC
static_cast<const T*>(this)
I'm fairly sure this is unsafe.
What prevents someone from doing this?
1 2 3 4 5
//class A:GeometryPlace{...};
A foo;
CircleArc2 ca;
ca.intersect(&foo);
Why do you want to avoid RTTI?
Topic archived. No new replies allowed.