I’ve overloaded the operator- (and -=) for 2D vectors (and scalars):
Vector2D Vector2D::operator-(const Vector2D& rhs) {
return Vector2D(this->GetX() - rhs.GetX(), this->GetY() - rhs.GetY());
}
In order to get this call to not complain that no operator '-' matches these arguments I had to do this:
a2de::Vector2D Vector2D::GetFacingVector(const Vector2D& target, const Vector2D& source) {
a2de::Vector2D facingVec(const_cast<Vector2D&>(target) - const_cast<Vector2D&>(source));
return facingVec;
}
Or this:
a2de::Vector2D Vector2D::GetFacingVector(const Vector2D& target, const Vector2D& source) {
a2de::Vector2D facingVec(Vector2D(target) - Vector2D(source));
return facingVec;
}
Both of which seem like a very bad thing to do. (Not-with-standing the phrase “only use const_cast when you know what you’re doing because the compiler will assume you do!”)
Are these correct or is there a better way to accomplish the same thing?
Your operator should be:
Note the const.