Zum Beispiel
VertexA+VertexB = VectorC //Wird nicht so oft verwendet...
VertexA-VertexB = VectorC //Könnte sehr häufig verwendet werden
VertexA+VectorB = VertexC //Könnte sehr häufig verwendet werden
VertexA-VectorB = VertexC //Könnte sehr häufig verwendet werden
VectorA+VectorB = VectorC //verwendet
VectorA-VectorB = VectorC //verwendet
VectorA+VertexB = VertexC //verwendet
VectorA-VertexB = VertexC //used
Wenn Sie feststellen werden, dass es eine zirkuläre Abhängigkeit gibt. Damit die Operatoren einer Klasse als Wert zurückkehren (nicht als Referenz oder Zeiger)
Ich kenne eine Lösung, Scheitelpunkte einfach als Vektoren auszudrücken. Allerdings habe ich mich gefragt, ob es eine andere Lösung gibt, weil mir die beiden verschiedenen Klassen nur aus Gründen der Übersichtlichkeit gefallen.
Code: Select all
#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;
}
};