The short answer is no, you can't do that.
For a 2D vector that you can access as an array, I would do something like this:
struct Vec2 {
float x;
float y;
const float &operator[](const size_t i) const {
static_assert(sizeof(Vec2) == 2 * sizeof(float));
assert(i < 2);
return (&x)[i];
}
float &operator[](const size_t i) {
return const_cast<float &>(std::as_const(*this)[i]);
}
};
So you can access the member variables directly or use the overloaded subscript operator.
Vec2 v{4, 5};
v.x += 9;
v[1] = -3;
In general, getters and setters are called explicitly. A naming convention I've seen quite a lot of is giving the getter and setter the same name.
class Foo {
public:
int member() const {
return hidden;
}
void member(const int value) {
hidden = value;
}
private:
int hidden;
};
This naming convention makes access very clean but still makes it clear that a function call is taking place.
Foo f;
f.member(5);
int five = f.member();