In C ++, operator-> has special semantics, because if the return type is not a pointer, it will again call operator-> on that type. But the intermediate value is stored as a temporary calling expression. This allows the code to detect changes in the return value:
template<class T> class wrapper { // ... T val; struct arrow_helper { arrow_helper(const T& temp) : temp(temp){} T temp; T* operator->() { return &temp; } ~arrow_helper() { std::cout << "modified to " << temp << '\n'; } }; arrow_helper operator->() { return{ val }; } //return const value to prevent mistakes const T operator*() const { return val; } }
and then the T elements can be accessed transparently:
wrapper<Foo> f(); f->bar = 6;
Is there anything that could go wrong how to do this? Also, is there a way to get this effect with functions other than operator-> ?
EDIT: Another issue I ran into is in expressions like
f->bar = f->bar + 6;
because when arrow_helper from the second operator-> destroyed, it again overwrites the value back to the original. My semi-elegant solution for arrow_helper have hidden T orig and assert(orig == *owner) in the destructor.
source share