我刚刚开始涉足c ++。这是向量3的正确实现吗?我不想泄漏任何内存,或者在c ++中做任何不好的事情。我的操作员超载签名是否正确?
class Vector
{
private:
double _x;
double _y;
double _z;
double LengthSquared();
friend std::ostream& operator<<(std::ostream& stream, const Vector& vector);
public:
Vector();
Vector(double x, double y, double z);
~Vector();
Vector(Vector& other);
double GetX() const;
double GetY() const;
double GetZ() const;
double Length();
Vector& Normalize();
double DotProduct(const Vector& vector) const;
Vector CrossProduct(const Vector& vector);
bool operator==(const Vector& vector) const;
bool operator!=(const Vector& other) const;
Vector& operator+=(const Vector& vector);
Vector operator+(const Vector& vector) const;
Vector& operator-=(const Vector& vector);
Vector operator-(const Vector& vector) const;
Vector& operator*=(double val);
double operator*(const Vector& vector) const;
Vector operator*(double val) const;
};
Vector::Vector() : _x(0.0), _y(0.0), _z(0.0)
{
}
Vector::Vector(double x, double y, double z) : _x(x), _y(y), _z(z)
{
}
Vector::~Vector()
{
}
Vector::Vector(Vector& other) : _x(other._x), _y(other._y), _z(other._z)
{
}
double Vector::GetX() const
{
return _x;
}
double Vector::GetY() const
{
return _y;
}
double Vector::GetZ() const
{
return _z;
}
double Vector::Length()
{
return sqrt(LengthSquared());
}
double Vector::LengthSquared()
{
return (_x * _x + _y * _y + _z * _z);
}
Vector& Vector::Normalize()
{
double length = Length();
if(length >0)
{
_x = _x / length;
_y = _y / length;
_z = _z / length;
}
else
{
_x = 0;
_y = 0;
_z = 0;
}
return *this;
}
double Vector::DotProduct(const Vector& vector) const
{
return _x * vector.GetX() + _y * vector.GetY() + _z * vector.GetZ();
}
Vector Vector::CrossProduct(const Vector& vector)
{
double nx = _y * vector.GetZ() - _z * vector.GetY();
double ny = _z * vector.GetX() - _x * vector.GetZ();
double nz = _x * vector.GetY() - _y * vector.GetX();
return Vector(nx, ny, nz);
}
bool Vector::operator==(const Vector& vector) const
{
if(this == &vector)
return true;
if((_x == vector.GetX()) && (_y == vector.GetY()) && (_z == vector.GetZ()))
return true;
return false;
}
bool Vector::operator!=(const Vector& vector) const
{
return !(*this == vector);
}
Vector& Vector::operator+=(const Vector& vector)
{
_x += vector.GetX();
_y += vector.GetY();
_z += vector.GetZ();
return *this;
}
Vector Vector::operator+(const Vector& vector) const
{
return Vector(_x, _y, _z) += vector;
}
Vector& Vector::operator-=(const Vector& vector)
{
_x -= vector.GetX();
_y -= vector.GetY();
_z -= vector.GetZ();
return *this;
}
Vector Vector::operator-(const Vector& vector) const
{
return Vector(_x, _y, _z) -= vector;
}
Vector& Vector::operator*=(double val)
{
_x *= val;
_y *= val;
_z *= val;
return *this;
}
double Vector::operator*(const Vector& vector) const
{
return this->DotProduct(vector);
}
Vector Vector::operator*(double val) const
{
return Vector(_x, _y, _z) *= val;
}
std::ostream& operator<<(std::ostream& stream, const Vector& vector)
{
return stream << "x: " << vector._x << ", y: " << vector._y << ", z: " << vector._z;
}
答案 0 :(得分:2)
_
领导的许多名称不是。Length
和LengthSquared
应为const
。DotProduct
和CrossProduct
非会员,非朋友“算法”功能。还要考虑非变异运算符(可以用规范形式T operator+(T left, const T& right) { return left += right; }
operator*
,因为它不直观,如果它意味着标量,点或交叉产品。改为使用命名方法。