I have two classes, a vertex and a vector, I am trying to use operators to make life simpler. If you'll examine the vector and vertex classes presented below I'm trying to implement operators in both vertex and vector.
For example VertexA+VertexB = VectorC //Isn't used that much...
VertexA-VertexB = VectorC //Could be used very frequently
VertexA+VectorB = VertexC //Could be used very frequently
VertexA-VectorB = VertexC //Could be used very frequently
VectorA+VectorB = VectorC //used
VectorA-VectorB = VectorC //used
VectorA+VertexB = VertexC //used
VectorA-VertexB = VertexC //used
If you'll notice there is a circular dependency. In order for the operators of one class to return by value( not by reference or pointer)
I know one work around, express vertexes just as vectors. However I was wondering if there was a different solution because I like the two different classes just for clarity.
#ifndef decimal
#ifdef PRECISION
#define decimal double
#else
#define decimal float
#endif
#endif
class Vector;
class Vertex{
public:
decimal x,y;
const Vertex operator+(const Vector &other);
const Vertex operator-(const Vector &other);
const Vector operator+(const Vertex &other);
const Vector operator-(const Vertex &other);
};
class Vector{
public:
decimal x,y;
const Vector operator+(const Vector &other) const {
Vector result;
result.x=this->x+other.x;
result.y=this->y+other.y;
return result;
}
const Vector operator-(const Vector &other) const {
Vector result;
result.x=this->x-other.x;
result.y=this->y-other.y;
return result;
}
const Vertex operator+(const Vertex &other) const {
Vertex result;
result.x=this->x+other.x;
result.y=this->y+other.y;
return result;
}
const Vertex operator-(const Vertex &other) const {
Vertex result;
result.x=this->x-other.x;
result.y=this->y-other.y;
return result;
}
decimal dot(const Vector &other) const{
return this->x*other.x+this->y*other.y;
}
const decimal cross(const Vector &other) const{
return this->x*other.y-this->y*other.x;
}
};