Sunday, 18 August 2013

How does this operator [ ] overloading works in this case?

How does this operator [ ] overloading works in this case?

I stumbled upon this class:
class Vec3f
{
...
float x, y, z;
...
};
inline float opertator[](const int index) const
{
return (&x)[index];
}
inline float& opertator[](const int index)
{
return (&x)[index];
}
The class is using the [] to access to x, y, z values as in an array so
that v[0] is the value in x, v[1] is the value in y, v[2] is the value in
z, but
How does the return statement working?
Is it correct to read it like: "get the value in the address specified by
index starting from the address of x"?
Do (&x) must be in parenthesis, otherwise it would return the value of the
address of x[index], isn't it?

No comments:

Post a Comment