A possible solution can be based on the inner class, the outer one is different, like the following:
struct S { template<typename T> class Prop { friend struct S; T t; void operator=(T val) { t = val; } public: operator const T &() const { return t; } }; void f() { prop = 42; } Prop<int> prop; }; int main() { S s; int i = s.prop;
As shown in the example, the class S
can change the property from its member functions (see S::f
). On the other hand, a property cannot be changed in any other way, but is still read using this operator, which returns a const reference to the actual variable.
source share