Original Post
I ran into a bit of a problem with overloading the assignment operator: something didn't work the way I expected to. I wanted to use it to convert between types. For example, the following code shows structures for 2D and 3D vectors, and simple operator overloads to convert between types: The problem is, the following doesn't seem to work: The code above gives the error "cannot convert from 'const Horizon::VECTOR3' to 'Horizon::VECTOR2'", but the code below works: Anyone have an explanation of why one works and the other doesn't? Also, is there anything that can be done to make the first snippet also work? Thanks!
struct VECTOR2;
struct VECTOR3;
struct VECTOR2
{
float x;
float y;
VECTOR2& operator=(const VECTOR3& in);
};
struct VECTOR3
{
float x;
float y;
float z;
VECTOR3& operator=(const VECTOR2& in);
};
VECTOR2& VECTOR2::operator =(const VECTOR3 ∈)
{
x = in.x;
y = in.y;
return *this;
}
VECTOR3& VECTOR3::operator =(const VECTOR2 ∈)
{
x = in.x;
y = in.y;
z = 0.0f;
return *this;
}
VECTOR2 v1;
v1.x = 5;
v1.y = 7;
VECTOR3 v2 = v1;
VECTOR2 v1;
v1.x = 5;
v1.y = 7;
VECTOR3 v2;
v2 = v1;