을 heres 내 수업연산자 오버로딩 이상한 결과
class Vector
{
public:
Vector();
Vector(float x, float y, float z);
float x;
float y;
float z;
Vector &operator+(const Vector &other) const;
Vector &operator+=(const Vector &other);
Vector &operator*(float n) const;
};
//op overloading functions impl
Vector &Vector::operator+(const Vector &other) const
{
Vector result = *this;
result.x += other.x;
result.y += other.y;
result.z += other.z;
return result;
}
Vector &Vector::operator+=(const Vector &other)
{
this->x += other.x;
this->y += other.y;
this->z += other.z;
return *this;
}
Vector &Vector::operator*(float n) const
{
Vector result = *this;
result.x *= n;
result.y *= n;
result.z *= n;
return result;
}
내가 잘못된 결과를 얻고있다. 예를 들어이 작동합니다
Vector vChange = velocity * time;
position += vChange;
동안이 나던 :
position += velocity * time;
가 컴파일하고 실행하지만,이 하나
같은 위치에 일부 가짜를 기록, 즉 :
Vector& Reflect(const Vector& I, const Vector& N)
{
Vector v = I - 2 * Dot(N, I) * N;
}
내가 뭘 잘못하고 있는지 조언 해 줄 수 있니? 감사!
[연산자 오버로딩 FAQ] (http://stackoverflow.com/q/4421706/46642)를 읽어 보는 것이 좋습니다. –