我使用两个Points 来定义 aLine和 a LineSegment,例如:
class Point { ... };
class Line
{
Point p1, p2;
//...
};
class LineSegment
{
Point p1, p2;
//...
};
LineSegment与 具有相同的定义Line,所以我一开始使用typedef Line LineSegment而不是定义另一个LineSegment类。但是很快,我发现我无法定义函数distance来计算点与线或点与线段之间的距离。
class Point { ... };
class Line
{
Point p1, p2;
//...
};
typedef Line LineSegment;
double distance(const Point& a, const Line& b) { ... }
double distance(const Point& a, const LineSegment& b) { ... }
当然我会得到一个编译错误。
所以,我的问题是:有没有更好的方法来区分Line没有LineSegment重新定义LineSegment?