在c ++中正确实现Vector 3

时间:2013-06-07 18:23:38

标签: c++

我刚刚开始涉足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;
}

1 个答案:

答案 0 :(得分:2)

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