Re: class member access operator (->) overloading
struct do_nothing { void pre(){}; void post(){} };
template<typename T, typename U = T*,
typename prepost_func = do_nothing>
class ptrproxy
{
T* ptr;
public:
explicit ptrproxy(T* p) : ptr(p)
{ prepost_func().pre(); }
~ptrproxy()
{ prepost_func().post(); }
U operator->() const {return U(ptr);}
};
template<typename T, typename PrePost>
struct funky_pointer_metaf {
typedef ptrproxy<T,ptrproxy<T,T*,PrePost> > type;
};
#include <iostream>
struct S {
void blah()
{ std::cout << " --- S::blah ---\n"; }
};
struct my_prepost {
void pre() { std::cout << "prior access\n"; }
void post() { std::cout << "after access\n"; }
};
int main() {
S s;
typedef funky_pointer_metaf<S,my_prepost>::type funky_t;
funky_t p (&s);
p->blah();
p->blah();
}
which outputs:
prior access
--- S::blah ---
after access
prior access
--- S::blah ---
after access
?
Cheers!
SG
Ok, It certainly does what I was looking for, lets see if I understand
it correctly.
You create a temporary sptr inside the original sptr return, that way
you can execute code using the temporary constructor and destructor
routines.
Smart, with this kind of method I'd only need to be careful to not use
throwable code inside the code intended to be executed after the call,
isn't it?
Thanks
Arkaitz