gpt4 book ai didi

c++ - 在 C++ 中正确实现 Vector 3

转载 作者:行者123 更新时间:2023-11-30 04:17:37 29 4
gpt4 key购买 nike

我刚刚开始接触 C++。这是 vector 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;
}

最佳答案

  • 我会避免在您的属性名称中使用前导下划线。这些特定名称是合法的,但由 _ 领导的许多名称不是合法的。
  • LengthLengthSquared 应该是 const
  • 编译器生成的复制构造函数和析构函数会做正确的事情 - 无需自己编写它们并冒着复制粘贴错误的风险。
  • 考虑使 DotProductCrossProduct 成为非成员、非友元“算法”函数。还要考虑非变异运算符的这一点(然后可以将其写成规范形式 T operator+(T left, const T& right) { return left += right; }
  • 强烈考虑完全不提供 operator*,因为如果它表示标量、点或叉积,则不直观。请改用命名方法。

关于c++ - 在 C++ 中正确实现 Vector 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16990599/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com