Skip to content

Instantly share code, notes, and snippets.

@zerotri
Created September 24, 2011 15:05
Show Gist options
  • Save zerotri/1239431 to your computer and use it in GitHub Desktop.
Save zerotri/1239431 to your computer and use it in GitHub Desktop.
I have no idea what is wrong with this. Trying to assign a new vector doesn't seem to change the values. Values are only changed using the initial constructor or if manually accessing members.
#include <math.h>
#include <iostream>
class Vector
{
public:
float x;
float y;
// constructor
Vector()
: x(0),y(0)
{
}
Vector(float _x, float _y)
: x(_x),y(_y)
{
}
// modifying operators
Vector operator =(const Vector &rhs)
{
return Vector(rhs.x, rhs.y);
}
Vector operator*=(const Vector &rhs)
{
return Vector(x*rhs.x, y*rhs.y);
}
Vector operator/=(const Vector &rhs)
{
return Vector(x/rhs.x, y/rhs.y);
}
Vector operator+=(const Vector &rhs)
{
return Vector(x+rhs.x, y+rhs.y);
}
Vector operator-=(const Vector &rhs)
{
return Vector(x-rhs.x, y-rhs.y);
}
Vector operator*=(const float rhs)
{
return Vector(x*rhs, y*rhs);
}
Vector operator/=(const float rhs)
{
return Vector(x*rhs, y*rhs);
}
bool operator ==(const Vector &rhs)
{
return ((x==rhs.x)&&(y==rhs.y));
}
bool operator !=(const Vector &rhs)
{
return ((x!=rhs.x)||(y!=rhs.y));
}
Vector operator =(const Vector &rhs) const
{
return Vector(rhs.x, rhs.y);
}
Vector operator -(const Vector &rhs) const
{
return Vector(x - rhs.x, y - rhs.y);
}
Vector operator +(const Vector &rhs) const
{
return Vector(x + rhs.x, y + rhs.y);
}
Vector operator*(const float scale) const
{
return Vector(x*scale, y*scale);
}
};
int main(int argc, char** argv)
{
Vector test(32,64);
test = Vector(12,0);
std::cout << "Vector: [" << test.x << "," << test.y << "]" <<std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment