Thinking about a way to simulate C#'s properties in C++, I came to the following solution:
#include <iostream>
class obj_with_property {
private:
class mykey {};
public:
class int_property {
private:
int m_v;
public:
int_property (int v, mykey) : m_v (v) {
}
int_property & operator = (int v) {
m_v = v;
return * this;
}
operator int () const {
return m_v;
}
};
int_property A;
obj_with_property () : A (int_property (0, mykey ())) {
}
};
int main(int argc, char **argv) {
obj_with_property obj;
std::cout << obj.A << std::endl;
obj.A = 25;
std::cout << obj.A << std::endl;
return 0;
}
I guess this approach could be improved further, e.g. by making int_property
a template etc. Now I cannot imagine I'm the first one to have this idea. Does anybody know whether a similar approach has been discussed anywhere?