I was converting a struct to a class so I could enforce a setter interface for my variables.
I did not want to change all of the instances where the variable was read, though.
So I converted this:
struct foo_t {
int x;
float y;
};
to this:
class foo_t {
int _x;
float _y;
public:
foot_t() : x(_x), y(_y) { set(0, 0.0); }
const int &x;
const float &y;
set(int x, float y) { _x = x; _y = y; }
};
I'm interested in this because it seems to model C#'s idea of public read-only properties.
Compiles fine, and I haven't seen any problems yet.
Besides the boilerplate of associating the const references in the constructor, what are the downsides to this method?
Any strange aliasing issues?
Why haven't I seen this idiom before?