Re: class member access operator (->) overloading
Arkaitz Jimenez wrote:
What sort of control are you after? If you just want to execute some
code after the return, I guess you could always just return an instance
of another class with an overloaded operator->, which itself executes
some code and returns the actual pointer. That code would then be
executing after the original return. But I'm not sure I understand the
motivation for doing this in the first place...
Regards,
Stu
Well, from what I know it's not possible, but just wondering if any
other trick was available.
The idea would be for example to wrap and mutexprotect one object
before and after a member is accessed or a method is executed.
Wraped<myobject> wrapobj;
wrapobj->whatever();
--------------------------
template operator -> would do:
mutex_lock(objolock);
wrapobj->called_method();
mutex_unlock(objlock);
Clearly there is no way of doing that with operator overloading, that
I know at least, but wanted to ask.
Well, off the top of my head, return a proxy object whose constructor
locks the mutex and whose destructor unlocks it. In addition, the proxy
object should have an operator-> that returns a pointer to the actual
object.
But be careful: this is rarely the right approach for robust
multi-threaded applications. If you've determined that it's what you
need, fine; but if it's more a "wouldn't it be nice to be able to do
this" thing, then you might want to think about it some more. Usually
what's needed is locking higher up: lock, read some data from one
object, store the data in another object, unlock. In that sequence,
individual locks on the read and the write are pointless.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)