У меня есть два класса: вершина и вектор, я пытаюсь использовать операторы, чтобы упростить жизнь. Если вы изучите классы векторов и вершин, представленные ниже, я пытаюсь реализовать операторы как в вершинах, так и в векторах.
Например
VertexA+VertexB = VectorC //Не так часто используется...
VertexA-VertexB = VectorC //Можно использовать очень часто
ВершинаA+ВекторB = VertexC //Можно использовать очень часто
VertexA-VectorB = VertexC //Можно использовать очень часто
VectorA+VectorB = VectorC //используется
VectorA-VectorB = VectorC //используется
VectorA+VertexB = VertexC //используется
VectorA-VertexB = VertexC //используется
Если вы заметили циклическую зависимость. Чтобы операторы одного класса возвращали значения по значению (а не по ссылке или указателю)
Я знаю один обходной путь: выражайте вершины как векторы. Однако мне было интересно, есть ли другое решение, потому что мне нравятся два разных класса просто для ясности.
#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;
}
};
Подробнее здесь: https://stackoverflow.com/questions/174 ... nd-vectors