On Friday, 6 September 2019 at 09:14:31 UTC, Andrew Edwards wrote:
C++ allows the for following:
struct Demo
{
float a, b, c, d;
Demo() { a = b = c = d = 0.0f; }
Demo(float _a, float _b, float _c, float _d) {
a = _a;
b = _b;
c = _c;
d = _d;
}
float operator[] (size_t i) const { return (&a)[i]; } //[3]
float& operator[] (size_t i) { return (&a)[i]; } //[4]
}
This is my thought on how to accomplish op overloading:
struct Test
{
float a, b, c, d;
float opIndex(size_t i)
in(i >= 0 && i <= 3)
{
final switch(i)
{
case 0: return a;
case 1: return b;
case 2: return c;
case 3: return b;
}
}
void opIndexAssign(float val, size_t i)
in(i >= 0 && i <= 3)
{
final switch(i)
{
case 0: a = val; break;
case 1: b = val; break;
case 2: c = val; break;
case 3: d = val; break;
}
}
}
Please advise if I've gone off the beaten path. It seems overkill
for the two lines of code C++ requires so any suggestion is
greatly appreciated.