M
Mark P
I working frequently with similar but distinct geometric objects. For
example, a collection of Edge classes. All Edge classes should support
certain basic functions, such as getTail(), getTip, getLength, etc. The
implementation may vary however: I may store tail and tip as Point
objects or as pairs of integers, I may store length explicitly or
compute it dynamically, etc.
Now what I'd like is to be able to provide a common interface of useful
functions to any Edge implementation that will work with any other Edge
implementation. For example, bool intersects (const Edge& e) const; to
determine if two Edges intersect.
One approach is an ABC AbstractEdge from which all Edge implementations
derive. The ABC would declare the pure virtual functions getTail(),
getTip(), etc. and would define the function intersects (const
AbstractEdge& ae) const; in terms of those functions.
However I don't need any of the dynamic dispatch features of virtual
functions and would rather express this in some sort of templatized
(i.e., compile-time resolved) structure. For example, something along
these lines (though this does not work as written):
template <class EType1>
class EInterface
{
public:
template <class EType2>
bool intersects (const Etype2& e) const
{
// some definition using functions defined in the Edge classes
}
};
class Edge1 : public EInterface<Edge1>
{
public:
// define all functions needed by intersects()
};
class Edge2 : public EInterface<Edge2>
{
public:
// define all functions needed by intersects()
};
int main()
{
Edge1 e1(args);
Edge2 e2(args);
e1.intersects(e2); // doesn't work
}
The intent is that every Edge implementation defines a basic set of
functions and then can snap in a common interface of more advanced
functions that rely on those basic functions. But as I said, the above
doesn't work-- is there a way to do this?
Thanks,
Mark
example, a collection of Edge classes. All Edge classes should support
certain basic functions, such as getTail(), getTip, getLength, etc. The
implementation may vary however: I may store tail and tip as Point
objects or as pairs of integers, I may store length explicitly or
compute it dynamically, etc.
Now what I'd like is to be able to provide a common interface of useful
functions to any Edge implementation that will work with any other Edge
implementation. For example, bool intersects (const Edge& e) const; to
determine if two Edges intersect.
One approach is an ABC AbstractEdge from which all Edge implementations
derive. The ABC would declare the pure virtual functions getTail(),
getTip(), etc. and would define the function intersects (const
AbstractEdge& ae) const; in terms of those functions.
However I don't need any of the dynamic dispatch features of virtual
functions and would rather express this in some sort of templatized
(i.e., compile-time resolved) structure. For example, something along
these lines (though this does not work as written):
template <class EType1>
class EInterface
{
public:
template <class EType2>
bool intersects (const Etype2& e) const
{
// some definition using functions defined in the Edge classes
}
};
class Edge1 : public EInterface<Edge1>
{
public:
// define all functions needed by intersects()
};
class Edge2 : public EInterface<Edge2>
{
public:
// define all functions needed by intersects()
};
int main()
{
Edge1 e1(args);
Edge2 e2(args);
e1.intersects(e2); // doesn't work
}
The intent is that every Edge implementation defines a basic set of
functions and then can snap in a common interface of more advanced
functions that rely on those basic functions. But as I said, the above
doesn't work-- is there a way to do this?
Thanks,
Mark