lilburne said:
Points and vectors are our stock in trade. Most of the time we don't
have any real problems with keeping them seperate and converting a Point
to a Vector and back again as needed, besides our vector class has a
cached length too. Heck we even have a UnitVector class too. For us the
important thing is clarity in the code, and a minimal use of casting. If
we really need to get jiggywithit these things get converted to raw arrays.
For the initial example given by the OP, we can keep Vector and Point
classes as separate types, whilst still avoiding the need for casts, by
having them implement an interface.
That is, both class can inherit a pure abstract class.
class ICoordinate {
public:
double x() = 0;
double y() = 0;
double z() = 0;
}
class Point : public ICoordinate {
public:
double x();
double y();
double z();
Vector release();
};
class Vector : public ICoordinate {
public:
double x() { return i(); )
double y() { return j(); }
double z() { return K(); }
double i();
double j();
double k();
Point anchor();
};
The code that would have needed to cast a Vector to a Point, would
actually use a pointer/reference of type ICoordinate, and simply call
the appropriate interface method.
It doesn't address how we access Point or Vector specific methods like
Point::anchor() or Vector::release(), but these methods are not the same
so it would not be appropriate to use an Interface for them.
Andrew